Wednesday, May 02, 2012

How to batch convert 1-up photos to 2-up in Mac OS X

I've recently started printing photos again and wanted to get more bang for my buck. Instead of printing 200 photos of a normal size, I wanted to print 100 photos normal size, plus 100 of them 2-up, halfsize. The benefit? Only paying for 150 prints, and having a variety of photo sizes to go into my scrapbook.

In other words,

These:




Become:


Here's a script I wrote to convert those 100 photos into 50 2-up photos.


#!/bin/python


import os


# Create output directory
outputDir = 'output'
directory = os.getcwd()
try:
    os.makedirs(outputDir)
except OSError:
    pass


# Generate 2-up photos
counter = 0
prevFile = ''
curFile = ''
for root, dirs, files in os.walk(directory):
for f in files:
filename, extension = os.path.splitext(str(f))
if (extension.lower() == '.jpg'): 
counter = counter + 1
if counter % 2 == 0:
curFile=filename
cmd = 'montage '+ prevFile + '.jpg ' + curFile + '.jpg -geometry +2+2 -tile 1x2 ' + outputDir+'/'+ prevFile+curFile + '.jpg'
print cmd
os.system(cmd)
else:
prevFile = filename

How to use it:

1. Install ImageMagick, making sure to do all the export commands in Terminal.
2. Using Terminal, change into the directory containing the photos you want to convert.
3. Create a file called script.py in that directory and paste the above code into it.
4. Run the script using python script.py and the 2-up photos will be placed into a folder called output.


Note: This works only on .jpgs for now but could easily work with any other photo format by replacing the .jpg's in the code above to whatever format you use. 


Thanks to Ben for the 2-up idea!

7 comments:

Greg said...

Image processing and shell scripting from Python? I'm so proud.

Angelica Lim said...

Python, it get things done (tm)

Kered said...

If there is an odd number of picture files, I guess the last one won't be processed?

Angelica Lim said...

It will be (with a white blank for the 2nd image), but the name won't be pretty ^^;

Kered said...

Oh really? I don't know Python, it's hard to spot where the "if" clauses end... but if there are 3 pictures files, during the last iteration of the "for" loop, the variable "counter" will be equal to 3, the condition "if counter %2 == 0" won't be met, we go to the "else" part and then exit the loop and the program without executing the command?

Angelica Lim said...

You're right. Damn. lol

Andrew Heather said...

You are awesome.