views:

204

answers:

1

I'm creating a python script to sort a lot of images (game screenshots).

I found a way to do that in imagemagick : I know that, if a specific square of the image is the same as the reference crop, then the image is of category one. If not, I check for another crop and another category, and if that doesn't fit either, I put the image in category three.

I found how to do that in Imagemagick :

convert file.jpg -crop 80x10+90+980 +repage crop.jpg
compare -metric PSNR reference.jpg crop.jpg crop.jpg

(I cut a piece of the image, then compare that piece to "reference.jpg")

How do I call that from the script, and do an if based on what convert returns (it's a number) ?

+3  A: 
import subprocess

retcode = subprocess.call(['convert', 'file.jpg', '-crop', 
                           '80x10+90+980', '+repage', 'crop.jpg'])
if retcode != 0:
    print 'error on convert'
else:
    retcode = subprocess.call(['compare', '-metric', 'PSNR', 
                               'reference.jpg', 'crop.jpg', 'crop.jpg'])
    print retcode
nosklo