0

sips is a command-line tool to process images on Mac OS X and BSD Unix. sips stands for Scriptable Image Processing System.

I have a 1-bit black and white uncompressed TIFF image. The following sips command compresses it with LZW compression.

sips -s format tiff -s formatOptions lzw sample.tif --out lzw.tif

However, the resultant TIFF image has black and white inverted when displayed on a certain viewer. This inversion is related to "Photometric Interpretation" as explained below.

Each pixel in a Black and White image is represented by a single bit. A 1-bit TIFF image has no palette, and can only be black and white. An "on" bit and an "off" bit can represent either black or white depending on the photometric tag (ID262) of the TIFF image. The typical photometric orientation of a black and white TIFF image is 0 = White and 1 = Black, which corresponds to a photometric tag value of 0. The opposite, 0 = Black, 1 = White corresponds to a photometric tag value of 1. Some image viewers ignore the photometric tag completely always assuming 0 = White, most notably the Windows XP Image Viewer. (source: atalasoft /KB/article.aspx?id=10069)

Black and white is inverted because the viewer prefers white to be 0, while sips uses white as 1 when generating the TIFF image.

The input file sample.tif has white as 0.
The output file lzw.tif has white as 1.

To check whether white is 0 or 1 (or whether 0 is white or black), that is, whether "Photometric Interpretation" is 0 or 1, at least the following two methods are available.

Use exiftool, which can be obtained from http://owl.phy.queensu.ca/~phil/exiftool/

exiftool -S lzw.tif -PhotometricInterpretation

Or use Preview.app on Mac OS X.

Tools > Show Inspector (Command-I) > TIFF

My question: How can sips be forced to use white as 0 when creating a 1-bit black and white TIFF image?

As I said, I have a 1-bit black and white uncompressed TIFF image. In fact, I have thousands of this kind. Thus, instead of manually processing them on GIMP or Photoshop, I need a command-line tool to automate the process.

If sips cannot do what I want, then I will perhaps post a new question asking for a command-line tool which
- runs on Mac OS X;
- takes a 1-bit black and white uncompressed TIFF image;
- outputs a 1-bit black and white LZW-compressed TIFF image with white as 0.

(My default shell is bash.)

Meanwhile, I will wait to see whether sips can.

As a sample image, I am trying to upload a 1-bit black and white uncompressed TIFF image, which consists of black letters on white background, and whose Photometric Interpretation is 0 (WhiteIsZero). However, the image-uploading mechanism at stackexchange automatically converts TIFF into PNG.
enter image description here

The sips command near the top of this post takes an uncompressed TIFF and outputs an LZW-compressed TIFF. The following sips command takes a PNG and outputs an LZW-compressed TIFF.

sips -s format tiff -s formatOptions lzw Cqdg2.png --out Cqdg2.tif

This resultant TIFF image Cqdg2.tif has the same problem as the earlier resultant TIFF image lzw.tif concerning the inversion and the Photometric Interpretation.

0

Probably most people will recommend that you use ImageMagick, which I understand is available for Mac OS X. On Linux I used it as follows to generate a tiff image similar to the one in your question, and to manipulate it to provide a compressed version with the Photometric option. Create the initial image:

$ convert -size 200x200 -depth 1 -gravity center -negate \
   label:UNIX a.tif 
$ file a.tif
a.tif: TIFF image data, little-endian, ... bps=1, \ 
 compression=none, PhotometricIntepretation=BlackIsZero, ...

Compress and change the colours with -negate or the Photometric with -define quantum:... as needed:

$ convert a.tif -define quantum:polarity=min-is-black -compress lzw b.tif
$ file b.tif
b.tif: ... compression=LZW, PhotometricIntepretation=BlackIsZero, ...

$ convert a.tif -define quantum:polarity=min-is-white -compress lzw b.tif
$ file b.tif
b.tif: ... compression=LZW, PhotometricIntepretation=WhiteIsZero, ...

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.