2
votes

I have several folders of 600 dpi TIFFs (CCITT Group IV, so black & white) that I need to convert to screen resolution PNGs - so in ImageMagick terms, I need to convert the format and resample the images to ~80 dpi. My first approach was to perform this in a single mogrify command (this is in bash on Mac OS X):

for folder in $(find * -maxdepth 0 -type d ); \
  do mogrify -path "$folder/medium" -format png -resample 31.5% "$folder/tiff/*.tif"; \
done

But the result was awful. The text in the resulting image was completely illegible. So I changed this to a two step process, (1) converting the TIFF to PNG at original resolution, then (2) downsizing the resolution:

for folder in $(find * -maxdepth 0 -type d ); \
  do mogrify -path "$folder/medium" -format png "$folder/tiff/*.tif"; \
  mogrify -resample 31.5% "$folder/medium/*.png"; \
done

While this process resulted in nice and crisp results at 80 dpi, the process was much slower, since I'm now writing the full resolution file to disk before downsizing the resolution.

Does anyone have a suggestion for the best way to accomplish a conversion and downsizing of resolution in a single step?

2
Have you looked into the sips command built into OSX?Digital Trauma
@DigitalTrauma No, I hadn't heard of sips before. Looks extremely convenient. I'll give it a shot. Thanks! These resources look helpful: developer.apple.com/library/mac/documentation/Darwin/Reference/… straylink.wordpress.com/2009/01/24/… and ainotenshi.org/818/resizing-images-using-the-command-lineJoe Wicentowski

2 Answers

1
votes

The sips tool can be used as follows:

sips -s format png -s dpiHeight 80 -s dpiWidth 80 -z 1200 1600 test.tiff --out test.png

Having said that in the resulting .png, the DPI settings don't seem to have been changed.

Also when resizing, it looks like you can only specify absolute pixel dimensions of the output image, and not a percentage of the input image. So you would have to grab the dimensions of the input image and calculate the new size explicitly:

#!/bin/bash

infile=test.tiff
outfile=test.png
pct=31 # only whole numbers for bash arithmetic

height=$(sips -g pixelHeight $infile | tail -1 | cut -d: -f2)
width=$(sips -g pixelWidth $infile | tail -1 | cut -d: -f2)
sips -s format png -s dpiHeight 180 -s dpiWidth 180 -z $((height*pct/100)) $((width*pct/100)) 1600 $infile --out $outfile
1
votes

I know I am late to the party, but I was looking at this and wondered why you get poor quality when doing both setps in one go. I wondered if it was maybe down to using mogrify rather than convert, so I set about trying to improve it. So, this would be my first and best attempt:

#!/bin/bash
for f in */tiff/*.tif; do
   out="${f%tif}png"        # replace "tif" suffix with "png"
   out=${out/tiff/medium}   # replace "tiff" directory with "medium"
   convert "$f" -resample 31.5% "$out"
done

And, if that still doesn't work, I could go for a second attempt which avoids writing the file to disk and then resampling it, and instead writes a PNG to stdout and then pipes that to a second convert that resamples and writes to disk - thereby avoiding the writing to disk of the big, intermediate PNG.

#!/bin/bash
for f in */tiff/*.tif; do
   out="${f%tif}png"        # replace "tif" suffix with "png"
   out=${out/tiff/medium}   # replace "tiff" directory with "medium"
   convert "$f" PNG:- | convert - -resample 31.5% "$out"
done