Posts Tagged ‘imagemagick’

How I prepare expense reports

Its been traditional since the old days to stick receipts on a sheet of paper and have them represent entries into an expense report. I think the only time this differed was at Sun – there we just stuffed it all into an envelope without any order and someone presumably went through all of them and figured out their own sane order.

Recently I had to scan about 70 pages worth of receipts (yes, imagine the expense value). My scanner creates some rather huge images (at 300dpi, with some 4128×2480 pixels), which average anywhere between 3-12MB per page for one JPG image. The scans to PDFs don’t make sense because I can’t concatenate PDFs the way I can JPG images (and the sheet feeder only handles about 15 or so sheets at once, reliably — its very easy to get paper jams when you have staples and loose paper).

Total size for everything? 300MB. A little hard to attach to an email. So I whipped out trusty ImageMagick (easily installable on MacOSX via homebrew — brew install imagemagick). ImageMagick wouldn’t install due to a conflict with pkg-config from Mono, and since I don’t use Mono any longer I trashed that.

Then it was as simple as:

ls -1 *.jpg | sed "s/\(.*\)\.jpg/\1.jpg \1_thumb.jpg/" | xargs -n 2 convert -resize 1024 -quality 70

What does that do? It simply finds all JPG files, then it saves thumbnails with a _thumb, and it uses convert (from ImageMagick) to resize it to 1024 pixels with only about 70% quality. This reduced the 300MB tome to something like 10MB!

How do I generate PDFs? Simply open all the thumbnails (open -a Preview *_thumb.jpg) and then do a Print to PDF from Preview.

Voila, all expense receipts are generated and ready for emailing. I typically like to keep attachments below 20MB in size because most mail systems reject it after that.

Identifying portrait/landscape in a set of images, with ImageMagick and BASH

I haven’t written much BASH of late, so was a bit rusty. The goal? A script that would go through a directory of JPGs, find those that are portrait shots, and place them in an appropriate folder. Do so similarly for landscapes.

Use cases? Those new digital photo frames. Buy two, and have many images scroll by, eh?

What’s been done?
Making use of ImageMagick’s identify is what needs to be done. You’ll be using the -format option, which normally takes a type or string. A full list of what options are available for -format are available.

The choice is to use -format '%[exif:orientation]'. The output of identify -format '%[exif:orientation]' is either:

  • 1 – portrait
  • 6 – landscape
  • 8 – portrait (the way I normally shoot, with the battery grip)

I’ve not seen much documentation about the above, so it seems like these values come from trial and error… They apply for Canon cameras that have EXIF orientation details. I’d be interested to hear from others what values they’re getting (or getting pointed to some documentation).

The shell script?


mkdir portrait
mkdir landscape

for i in *.jpg;
	type=$(/opt/local/bin/identify -format '%[exif:orientation]' $i)
case $type in
	mv $i portrait;;
	mv $i landscape;;
	mv $i portrait;;

Other bits of BASH?
If you do: type=$(/opt/local/bin/identify -format '%[exif:orientation]' $i) , you can grab the value from the command (1, 6, 8) and manipulate it. Check by doing echo $type.

If you’re after getting the return value from a command (i.e. 0 for success, 127 for error, and so on), you can do echo rv: $?.

All in all, remember to read the BashFAQ. Tests and Conditionals was also essential to my reading.

ImageMagick rocks nonetheless. I’ve used it before to resize, append and much more… I’m thinking that maybe its time to read The Definitive Guide to ImageMagick, written by Mikal.