My ImageMagick Adventure
GOAL:
I like how Flickr automatically generates six different sized photos on upload while preserving the original.
Not wanting to be dependent on Flickr for this, I wanted to learn how to do this automagically with my computer. With a developing awareness of the power of Unix, I realized that a command line function was probably the way to go.
So I learned about ImageMagick.
INSTALL:
The recommended method for install is through MacPorts, wherein you simply type
ports install ImageMagick
in Terminal, but I’d tried some MacPorts stuff over the summer which hadn’t worked so I avoided this in lieu of downloading the package from the ImagaMagick host site.
On install however, I immediately ran into a CPU error. This was because the download currently on offer for OS X is complied for 64bit processors, which is fine if you’re using a fairly new machine. My 2006 MacBook is still running with the now obsolete 32 bit processor. Googling this issue led me to a posting on Stack Overflow, where someone with the same problem figured out how to compile the binaries using one of the Unix distributions.
I tried this, but at first I couldn’t get it to work. The issue, it turned out, was that I didn’t have an install of Snow Leopard’s Xcode Developer tools. I’d installed the Developer tools years ago, and so have had the Developer folder on my hard drive, but I also had two hard drive crashes and a couple of system restores in between. This meant that while the files were represented, some of them were out of date, and while I’d updated my system to Snow Leopard, I hadn’t matched the update to the Developer tools.
So, using the Snow Leopard install disk, I ran the install process and got my system up to spec. I then figured I should try MacPorts again, suspecting that my earlier issues had just been resolved via my install of the Dev tools. This turned out to be the case: I installed MacPorts, and on complete, went to Terminal and typed the formula. The list of dependencies began to scroll down the screen, and after about an hour (seriously, it took a while), I had ImageMagick installed, one appropriate for my 32bit system.
However, I couldn’t get ‘display’ to function, but I realized this wasn’t necessary for my ultimate purpose.
MAGICK:
Now that I had my system ready for processing, the next step was to determine the code string to process, which for a relative Unix newbie like me, wasn’t intuitive.
Some Googling helped with this, and the site Perturb.org gave a list, from which I tried this one. First, I cd‘d into the folder that contained the copies of the images I wanted to work on. Then, confirming my location either with the ls or the pwd command, I typed:
for i in `ls *.jpg`; do convert -resize 50% -quality 80 $i conv_$i; done
This didn’t do what I ultimately wanted, but it allowed me to confirm that ImageMagick would work, and begin to give me some clues as to what was going on.
With the help of this post on Cubiq.org, I got this thumbnail script to generate a 75×75 square (as per Flickr’s size):
for i in `ls *.jpg`; do convert -size 75x75 -thumbnail 75x75^ -gravity center -extent 75x75 $i th_$i; done
How this works: i in `ls *.jpg`; that is, make i a variable ($i) containing the listing (ls) of the folder, specifically, the jpgs (*.jpg) of that folder (excluding any other that might exist) and then convert them to -size 75×75 (reiterate a thumbnail of 75×75) with the -gravity of the image centered and output as a file named th_(name in variable $i)
This takes a file named 001.jpg and outputs th_001.jpg
In addition, I had a whole folder of .tif’s which I wanted to convert first to .jpg. Therefore, I used mogrify, which comes with a warning. As it operates on your files it will overwrite the originals if you don’t output them to a folder, as I did. Needless to say, it is always best practice to process copies of images, and never the original/onlycopy.
I mkdir
a directory within the folder I cd’d into called jpg. Then, I typed:
mogrify -path jpg -format jpg *.tif
This command converted all the tif files (*.tif) and placed them in the “jpg” folder I’d created (via -path)
Transcribing to Human: use the mogrify command (-path jpg) and place in the jpg folder and change all the files to jpg (-format jpg) that are tiff (*.tiff)
I was now understood how this worked enough to modify it for my end use. I realized that ‘mogrify’ was a better command to use than the ‘convert’ and that I could make a directory as part of the initial conversion, by adding it to the string. I also looked into sharpening the thumbnails, as I was comparing the test runs against those generated by Flickr.
Here, you can see that the Flickr image is a little bit sharper.
I learned that the ‘unsharp’ command is incorrectly named because it is actually what we’d think of as the ‘sharpenning’ option. Thus, my new 75×75 thumbnail command read:
mkdir sq; for i in `ls *.jpg`; do mogrify -size 75x75 -thumbnail 75x75^ -gravity center -unsharp 0x1 -extent 75x75 -path sq $i sq_$i; done
Human: make a directory called `sq` and while the `i` variable is what you find when you do a `ls` that are jpgs, mogrify them to size 75×75 as a thumbnail with the gravity of the image on the centre, and then sharpen them, and make sure they are 75×75 and output them to the `sq` folder you just made, and save them as sq_(what-you-found-when-you-did-the-listing-of-the-folder-of-the-jpgs).
I was happy to see that `unsharp` produced a thumbnail that seemed visibly better than Flickr’s!
Thumbnails are a special case. For simple resizing, the script can also be pared down. I noticed that Flickr resizes according to set widths: 100, 240, 500, 640, 1024. The next issue I had to figure out was how to get ImageMagick to scale proportionally to a set width. I learned that if you give ImageMagick’s mogrify one size, it will assume it is the width and resize accordingly. Thus, to generate an image 100 pixels wide:
mkdir 100; for i in `ls *.jpg`; do mogrify -resize '100' -gravity center -unsharp 0x1 -path 100 $i sq_$i; done
From here, it’s just a matter of entering whatever size you want. As noted above, my script includes a mkdir folder called 100, but that isn’t required, it’s just good to have the output directed to a folder so that the files aren’t overwritten in the processing.
Finally, here are my current list of resizing abracadabra scripts.
mkdir sq; for i in `ls *.jpg`; do mogrify -size 75x75 -thumbnail 75x75^ -gravity center -unsharp 0x1 -extent 75x75 -path sq $i sq_$i; done
mkdir 100; for i in `ls *.jpg`; do mogrify -resize '100' -gravity center -unsharp 0x1 -path 100 $i sq_$i; done
mkdir 240; for i in `ls *.jpg`; do mogrify -resize '240' -gravity center -unsharp 0x1 -path 240 $i sq_$i; done
mkdir 500; for i in `ls *.jpg`; do mogrify -resize '500' -gravity center -unsharp 0x1 -path 500 $i sq_$i; done
mkdir 640; for i in `ls *.jpg`; do mogrify -resize '640' -gravity center -unsharp 0x1 -path 640 $i sq_$i; done
(The photo is of my father examining the body of his 1939 Oldsmobile in the driveway of our home in Etobicoke in the late 1970s).