Search code examples
pythonpython-imaging-librarytiff

PIL UnidentifiedImageError with TIFF


I have documents where each page is an individual tiff file. So i am using a python script to combine pages of the same document into a single multiframe tiff file. When i come to read some of my tiff files into memory using

opened_images_list.append(Image.open(directory_path + '\\' + f, formats = ['tiff']))

Then i get the error PIL.UnidentifiedImageError: cannot identify image file '\\windata1\data\IntelligentCapture\2015\01\26\42030593568-1'

If I open the image in paint and then save it as a TIFF i can then read it into PIL but this happens to be thousands of images.

I used tifffile to have a look at the image using

python -m tifffile '\\windata1\data\IntelligentCapture\2015\01\26\42030593568-1' 

and this was the output:

TiffFile '42030593568-1'  192.75 KiB

TiffPageSeries 0  2339x1648  uint8  YX  Generic  1 Pages

TiffPage 0 @8  2339x1648  uint8  minisblack ojpeg

TiffTag 254 NewSubfileType @10 LONG @18 = UNDEFINED
TiffTag 256 ImageWidth @22 LONG @30 = 1648
TiffTag 257 ImageLength @34 LONG @42 = 2339
TiffTag 258 BitsPerSample @46 SHORT @54 = 8
TiffTag 259 Compression @58 SHORT @66 = OJPEG
TiffTag 262 PhotometricInterpretation @70 SHORT @78 = MINISBLACK
TiffTag 273 StripOffsets @82 LONG @90 = (768,)
TiffTag 274 Orientation @94 SHORT @102 = TOPLEFT
TiffTag 277 SamplesPerPixel @106 SHORT @114 = 1
TiffTag 278 RowsPerStrip @118 LONG @126 = 2339
TiffTag 279 StripByteCounts @130 LONG @138 = (196608,)
TiffTag 282 XResolution @142 RATIONAL @760 = (200, 1)
TiffTag 283 YResolution @154 RATIONAL @752 = (200, 1)
TiffTag 284 PlanarConfiguration @166 SHORT @174 = CONTIG
TiffTag 296 ResolutionUnit @178 SHORT @186 = INCH
TiffTag 305 Software @190 ASCII[56] @696 = Kofax standard Multi-Page TIFF Storage Filter v3.03.000
TiffTag 306 DateTime @202 ASCII[20] @676 = 2015:01:26 10:48:34
TiffTag 513 JPEGInterchangeFormat @214 LONG @222 = (768,)
TiffTag 33000 @226 SHORT @234 = 0

What can i do to get PIL to read this image?


Solution

  • You don't want TIFFs with OJPEG compression, they are obsolete and poorly supported.

    I don't have any to test, but you should be able to detect which ones have this compression usiing ImageMagick with:

    magick identify -format "%f:%C\n" *.tif
    

    Sample Output

    blocks.tif:None
    floats.tif:None
    l.tif:LZW
    n.tif:None
    

    You can then use whatever tools your OS provides to get a list of the OJPEG ones.


    You can then convert them by batching/looping over those files. Here is how you change one file to LZW compression:

    magick INPUT.TIF -compress LZW OUTPUT.TIF
    

    I would change the "unhappy" ones to whatever compression the majority of your other ones use.


    You can get a list of all the compression types ImageMagick supports with:

    magick identify -list compress