2

I am scanning a rare old book (cookbook) for archiving using my Canon LiDE 110. The scans are .TIF files, and I want to batch crop them for uniformity, et cetera.

This is how I am doing the batch cropping using ImageMagick:

mogrify -gravity North -crop 1600x2512+0+0 -type Palette -define tiff:rows-per-strip=16 -define tiff:subfiletype=PAGE *.tif

(The additional command-line options such as type, rows-per-strip and subfiletype were used to keep the properties identical to the original images.)

I've also tried cropping each file at a time like so:

mogrify -gravity North -crop 1600x2512+0+0 -type Palette -define tiff:rows-per-strip=16 -define tiff:subfiletype=PAGE IMG_0003.tif

The problem is that the resultant (output) cropped images are all of the same size in bytes! (All are 4,022,366 bytes to be exact.)

Original Images [1]:

$ identify *.tif

IMG_0002.tif TIFF 1660x2572 1660x2572+0+0 16-bit sRGB 256c 4.273MB 0.000u 0:00.000
IMG_0003.tif TIFF 1652x2556 1652x2556+0+0 16-bit sRGB 256c 4.226MB 0.000u 0:00.000
IMG_0004.tif TIFF 1656x2572 1656x2572+0+0 16-bit sRGB 256c 4.262MB 0.000u 0:00.000
IMG_0005.tif TIFF 1668x2604 1668x2604+0+0 16-bit sRGB 256c 4.347MB 0.000u 0:00.000
IMG_0006.tif TIFF 1680x2544 1680x2544+0+0 16-bit sRGB 256c 4.277MB 0.000u 0:00.000

Cropped Images:

$ identify *.tif

IMG_0002.tif TIFF 1600x2512 1600x2512+30+0 16-bit sRGB 256c 4.022MB 0.000u 0:00.000
IMG_0003.tif TIFF 1600x2512 1600x2512+26+0 16-bit sRGB 256c 4.022MB 0.000u 0:00.000
IMG_0004.tif TIFF 1600x2512 1600x2512+28+0 16-bit sRGB 256c 4.022MB 0.000u 0:00.000
IMG_0005.tif TIFF 1600x2512 1600x2512+34+0 16-bit sRGB 256c 4.022MB 0.000u 0:00.000
IMG_0006.tif TIFF 1600x2512 1600x2512+40+0 16-bit sRGB 256c 4.022MB 0.000u 0:00.000

Why could this be happening?


Footnotes:

  1. For the sake of identifying the problem in the question (if any) I am temporarily making the original files publicly available, here:

    These will be deleted once the question has an acceptable answer/resolution Deleted.

5
+100

The mogrify command crops the image to a fixed size. It also removes any compression present in the image, mainly to avoid recompressing JPEG images and losing image quality. It also ensures the resulting TIFF is fully compatible with the baseline format.

The result is that you will get very similarly sized files for each of your scans:

x_pixels * y_pixels * bytes_per_pixel

In your case x_pixels=1600, y_pixels=2512, and bytes_per_pixel=1 because you've specified a fixed colour palette. Allowing for metadata and the palette map, the resulting file sizes will all be a little larger than 4,019,200 bytes:

-rw-r--r--+ 1 roaima users 4225562 Dec 30 16:28 IMG_0003.tif    # Original
-rw-r--r--+ 1 roaima users 4022354 Jan  5 09:55 IMG_0003.tif    # Mogrified

You can see that the metadata and palette take an additional 203,208 bytes above the theoretical minimum size.

Now, there are a number of extensions to the TIFF format, one of which allows for a number of different types of compression. These compression types can be applied with the -compress {type} flag to mogrify. Some of these compression types are lossy, others are lossless. Here are some comparative sizes for the lossless options:

-rw-r--r--+ 1 roaima users 4022354 Jan  5 10:02 IMG_0003.tif    # None
-rw-r--r--+ 1 roaima users 4022354 Jan  5 10:02 IMG_0003.tif    # BZip
-rw-r--r--+ 1 roaima users 446952 Jan  5 10:02 IMG_0003.tif     # LZW
-rw-r--r--+ 1 roaima users 594040 Jan  5 10:02 IMG_0003.tif     # RLE
-rw-r--r--+ 1 roaima users 429692 Jan  5 10:02 IMG_0003.tif     # Zip
-rw-r--r--+ 1 roaima users 478622 Jan  5 10:02 IMG_0003.tif     # LZMA

Unexpectedly, BZip compression appears to fail - at least on my system - but I have not been able to find any other instance of this reported via my preferred search engine.

The net result is that if you modify your mogrify command slightly you will get lossless TIFF files at a greatly reduced size:

mogrify -gravity North -crop 1600x2512+0+0 -type Palette \
-define tiff:rows-per-strip=16 -define tiff:subfiletype=PAGE -compress Zip *.tif

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.