python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.28k stars 2.23k forks source link

Grayscale PNG converted with standard tool to 8-bit grayscale TIFF won't load #7995

Closed dwvisser closed 6 months ago

dwvisser commented 6 months ago

What did you do?

I take a PNG file of rotated text, used for testing OCR, and convert it to 8-bit grayscale TIFF using the GraphicsMagick command-line utility. Then I try to load the resulting image into Pillow.

What did you expect to happen?

Successful load.

What actually happened?

Unrecognized file format exception.

What are your OS, Python and Pillow versions?

❯ python -m PIL --report
--------------------------------------------------------------------
Pillow 9.4.0
Python 3.8.19 | packaged by conda-forge | (default, Mar 20 2024, 12:47:35)
       [GCC 12.3.0]
--------------------------------------------------------------------
Python modules loaded from /home/dale/miniforge3/envs/ediscovery/lib/python3.8/site-packages/PIL
Binary modules loaded from /home/dale/miniforge3/envs/ediscovery/lib/python3.8/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 9.4.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.12.1
--- LITTLECMS2 support ok, loaded 2.15
--- WEBP support ok, loaded 1.3.2
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for 9.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.0
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.13
--- LIBTIFF support ok, loaded 4.5.0
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
BLP
Extensions: .blp
Features: open, save, encode
--------------------------------------------------------------------
BMP image/bmp
Extensions: .bmp
Features: open, save
--------------------------------------------------------------------
BUFR
Extensions: .bufr
Features: open, save
--------------------------------------------------------------------
CUR
Extensions: .cur
Features: open
--------------------------------------------------------------------
DCX
Extensions: .dcx
Features: open
--------------------------------------------------------------------
DDS
Extensions: .dds
Features: open, save
--------------------------------------------------------------------
DIB image/bmp
Extensions: .dib
Features: open, save
--------------------------------------------------------------------
EPS application/postscript
Extensions: .eps, .ps
Features: open, save
--------------------------------------------------------------------
FITS
Extensions: .fit, .fits
Features: open, save
--------------------------------------------------------------------
FLI
Extensions: .flc, .fli
Features: open
--------------------------------------------------------------------
FPX
Extensions: .fpx
Features: open
--------------------------------------------------------------------
FTEX
Extensions: .ftc, .ftu
Features: open
--------------------------------------------------------------------
GBR
Extensions: .gbr
Features: open
--------------------------------------------------------------------
GIF image/gif
Extensions: .gif
Features: open, save, save_all
--------------------------------------------------------------------
GRIB
Extensions: .grib
Features: open, save
--------------------------------------------------------------------
HDF5
Extensions: .h5, .hdf
Features: open, save
--------------------------------------------------------------------
ICNS image/icns
Extensions: .icns
Features: open, save
--------------------------------------------------------------------
ICO image/x-icon
Extensions: .ico
Features: open, save
--------------------------------------------------------------------
IM
Extensions: .im
Features: open, save
--------------------------------------------------------------------
IMT
Features: open
--------------------------------------------------------------------
IPTC
Extensions: .iim
Features: open
--------------------------------------------------------------------
JPEG image/jpeg
Extensions: .jfif, .jpe, .jpeg, .jpg
Features: open, save
--------------------------------------------------------------------
JPEG2000 image/jp2
Extensions: .j2c, .j2k, .jp2, .jpc, .jpf, .jpx
Features: open, save
--------------------------------------------------------------------
MCIDAS
Features: open
--------------------------------------------------------------------
MIC
Extensions: .mic
Features: open
--------------------------------------------------------------------
MPEG video/mpeg
Extensions: .mpeg, .mpg
Features: open
--------------------------------------------------------------------
MSP
Extensions: .msp
Features: open, save, decode
--------------------------------------------------------------------
PCD
Extensions: .pcd
Features: open
--------------------------------------------------------------------
PCX image/x-pcx
Extensions: .pcx
Features: open, save
--------------------------------------------------------------------
PIXAR
Extensions: .pxr
Features: open
--------------------------------------------------------------------
PNG image/png
Extensions: .apng, .png
Features: open, save, save_all
--------------------------------------------------------------------
PPM image/x-portable-anymap
Extensions: .pbm, .pgm, .pnm, .ppm
Features: open, save
--------------------------------------------------------------------
PSD image/vnd.adobe.photoshop
Extensions: .psd
Features: open
--------------------------------------------------------------------
SGI image/sgi
Extensions: .bw, .rgb, .rgba, .sgi
Features: open, save
--------------------------------------------------------------------
SPIDER
Features: open, save
--------------------------------------------------------------------
SUN
Extensions: .ras
Features: open
--------------------------------------------------------------------
TGA image/x-tga
Extensions: .icb, .tga, .vda, .vst
Features: open, save
--------------------------------------------------------------------
TIFF image/tiff
Extensions: .tif, .tiff
Features: open, save, save_all
--------------------------------------------------------------------
WEBP image/webp
Extensions: .webp
Features: open, save, save_all
--------------------------------------------------------------------
WMF
Extensions: .emf, .wmf
Features: open, save
--------------------------------------------------------------------
XBM image/xbm
Extensions: .xbm
Features: open, save
--------------------------------------------------------------------
XPM image/xpm
Extensions: .xpm
Features: open
--------------------------------------------------------------------
XVTHUMB
Features: open
--------------------------------------------------------------------

First,

gm convert -colorspace sRGB -background white -depth 8 -density 300 \
  -type Palette lorem_ipsum_90deg.png output.tif

Here is the input PNG file: lorem_ipsum_90deg

Try to load with Pillow

from PIL import Image
image = Image.open("output.tif")
print("Done.")

Resulting error:

➜  python open_rotated_loren_ipsum.py 
Traceback (most recent call last):
  File "/home/user/my_project/tests/open_rotated_loren_ipsum.py", line 3, in <module>
    image = Image.open("lorem_ipsum_90deg.tif")
  File ".../site-packages/PIL/Image.py", line 3339, in open
    raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file '/home/user/my_project/tests/output.tif'
radarhere commented 6 months ago

Would you be able to upload the TIFF image itself? It's possible you're using an older version of GraphicsMagick, and removing as much ambiguity as possible in bug reports is helpful.

dwvisser commented 6 months ago

Would you be able to upload the TIFF image itself? It's possible you're using an older version of GraphicsMagick, and removing as much ambiguity as possible in bug reports is helpful.

Sure! Here it is (zipped because GitHub blocks TIFF files): temp_tiff.zip

radarhere commented 6 months ago

Ok, I've created #7996. With it,

from PIL import Image
image = Image.open("temp_tiff.tif")
image.save("out.png")

gives

out

dwvisser commented 6 months ago

Thanks! I'll try the patch myself tomorrow. 😁