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])

CGI is the new real

For quite a while now I have had clients approach me to re-create existing products that they have failed to had photographed well. They are selling these products on Ebay, Amazon, their own websites and other online outlets and require high-quality, bright, sparkly and above oh-so-real images.

They can range from high-end watches to simple plastic utensils, with just about everything in between. CGI really is the new real these days…

LC gc render au01 CL render 09 JC cam render 15 0001 milkshake bar coffee RC render 15


Mogrify – more pleasant than it sounds

If you are lucky or smart enough to use Ubuntu, then you have a wealth of tools sitting just under your keyboard that you might know owt about.

Two of those are the convert and mogrify tools, which come from the Imagemagick stable. I use these quite frequently for batch conversion jobs.

Here is an example of what these tools can do, specifically mogrify which acts on batches of files.

I have a folder load of transparent images that the client wants on a white background, so I open the command line, navigate to that folder (or just open the command line in that folder directly using Nemo instead of Nautilus) and type the following incantation:

mogrify -format jpg -quality 100 -fill white -opaque none *.png

If we break this down into individual components, then

mogrify this is the command itself

-format jpg The output format, in this case jpg

-quality 100 for jpgs specifies the compression or quality levels. 100 is tops, 1 is rubbish.

-fill white the colour of the fill which will be determined by the next command.

-opaque none deals with the alpha (transparency) channel, by filling it with a colour, which has handily just been specified.

*.png simply tells mogrify to grab any file that ends in “.png”