1

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

  • In case you don't get a solution, you can always use **ImageMagick** at the commandline to convert either way, e.g. `convert input.tif -compress lzw output.tif` or `convert input.tif -compress none output.tif`. If using V7 or newer, use `magick` in place of `convert`, i.e. `magick input.tif -compress lzw output.tif`. – Mark Setchell Jan 05 '18 at 07:41
  • Can you share the file `Frame_0_369.tif`? – cgohlke Jan 05 '18 at 18:58
  • I've updated the question with an example image. Not the same image, but the problem is the same – Sondre Høglund Jan 08 '18 at 10:10

2 Answers2

2

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.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
2

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