Sondre Høglund
Sondre Høglund

Reputation: 53

Open uncompressed TIFF files in OpenCV python

I'm trying to open two different TIFF files in python using OpenCV function imread

image = cv2.imread(os.path.join(folder, file), -1)

The first file opens without any problem, but when I try to open the second file, imread returns 'None'. The only difference between the files, is that the second file is uncompressed.

Property page for both tiff images : Property page for both tiff images

I also tried to open the second file using PIL and matplotlib with no success.

Has anyone successfully opened an uncompressed 16-bit TIFF image in python?

Here's an example file. Download it and open with InfranView if you would look to view the image (Google Drive does not support viewing)

Best regards,
Sondre

Upvotes: 1

Views: 6377

Answers (2)

Sondre Høglund
Sondre Høglund

Reputation: 53

Found the solution to the problem, it is actually already answered here. Using tifffile module opened the image successfully.

Upvotes: 2

Mark Setchell
Mark Setchell

Reputation: 207405

Your image is not a valid TIFF file because it is missing the "Photometric Interpretation", i.e. tag 262.

You can see the various tags with tiffdump which comes with libtiff

tiffdump Background.tif

Output

Background.tif:
Magic: 0x4949 <little-endian> Version: 0x2a <ClassicTIFF>
Directory 0: offset 2887048 (0x2c0d88) next 0 (0)
ImageWidth (256) SHORT (3) 1<1388>
ImageLength (257) SHORT (3) 1<1040>
BitsPerSample (258) SHORT (3) 1<16>
Compression (259) SHORT (3) 1<1>
StripOffsets (273) LONG (4) 1040<8 2784 5560 8336 11112 13888 16664 19440 22216 24992 27768 30544 33320 36096 38872 41648 44424 47200 49976 52752 55528 58304 61080 63856 ...>
SamplesPerPixel (277) SHORT (3) 1<1>
RowsPerStrip (278) SHORT (3) 1<1>
StripByteCounts (279) LONG (4) 1040<2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 2776 ...>
PlanarConfig (284) SHORT (3) 1<1>
ResolutionUnit (296) SHORT (3) 1<1>

You can set the tag using tiffset utility that also comes with libtiff, for example:

tiffset -s 262 1 YourUnhappyImage.tif

Or, you can correct the application that produces it.

Upvotes: 2

Related Questions