Convert in python shell script

This is really more of a reminder for me than anything else.

Convert is a tool from the same stable as Mogrify, and is an absolute life-saver when working with large batches of images.

Just the other day I was using it to crop some 2000 jpegs which were scans of pages from a rather thick book. The odd and even pages were offset, by which I mean that the space on the outside of the page was greater than on the inside, and I wanted to crop the pages so that they looked pretty even.

Not wanting to do this all by hand, I wrote a Python shell script to process the files.


#!/usr/bin/env python3

import subprocess
import os
import sys

path_to_jpegs = "/home/dani/big fat book/pages/"

path_to_output = path_to_jpegs + 'cropped/'


file_path = path_to_jpegs

# loop through all the images in the specified folder
for filename in os.listdir(file_path):
    if ('.jpg' in filename):    
        print('@@@ processing file: ' + path_to_jpegs + filename)

        # file number is needed in order to determine of the file is an odd or even page
        dot_pos = filename.find('.')
        file_number = int(filename[:dot_pos])
        # switch coords depending if this file is even or odd
        # left page
        if (file_number % 2 == 0):
            #coords = '1940x2720+966+240'
            coords = '1940x2720+400+238'
        # right page
            #coords = '1940x2720+400+238'
            coords = '1940x2720+966+240'
       ['convert', '-crop', coords, path_to_jpegs + filename, path_to_output + filename])