Voici un petit exemple de code en python permettant d'appeler un processus externe.
Dans cet exemple, nous appelons "imagemagick" pour effectuer le redimensionnement des images contenues dans un répertoire vers deux dimensions.
#!/usr/bin/python
from glob import glob
from subprocess import check_call, CalledProcessError
import os
class ConvertError(Exception):
"""Error while trying to convert.
"""
root_path = '/home/user/Bureau/'
ext = '.jpg'
files = glob(root_path + '*' + ext)
try:
os.mkdir(root_path + "150/")
except OSError:
pass
try:
os.mkdir(root_path + "650/")
except OSError:
pass
for f in files:
folder_path, file_name = os.path.split(f);
img_150_path = root_path + '_150' + ext
img_650_path = root_path + '_650' + ext
try:
check_call(["convert", f, "-resize", "150x150", "-quality", "80", root_path + "150/" + file_name])
check_call(["convert", f, "-resize", "650x900", "-quality", "80", root_path + "650/" + file_name])
except CalledProcessError:
raise ConvertError("Error converting")
try:
os.renames(f, root_path + "ori/" + file_name)
except OSError:
raise ConvertError("Error renaming")