-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pillow 11.0.0 and Django: TiffImagePlugin raised 'Invalid dimensions' error #8530
Comments
Could you attach a copy of the image? |
no, sorry :( ... |
I'm guessing that you mean you can't for copyright/privacy reasons, but you still have a copy of the image you can test with? You can still replicate the problem on your end? Otherwise, you couldn't have determined that this passed with Pillow 10.4.0. Are you sure that the image isn't actually invalid? Are you able to open the image using other software? Are you able to modify your copy of Pillow to print If you're unable to upload the image, can you run |
Yes, it's about copyright. I can play with the image locally. It's very strange, i can't reproduce with: import sys
import PIL
from PIL import Image
print(f'{sys.version=}')
print(f'{PIL.__version__=}')
with open("/tmp/foobar.tiff", "rb") as fp:
im = Image.open(fp)
print(f'{im.size=}') It works in both cases:
exiftool:
But this runs on a different machine... |
I also tried with Python 3.11:
Strange. Any idea? |
If you're asking how Django could have manipulated the image before passing it to Pillow, then that sounds like a Django question more than a Pillow question. If you're asking how to debug this further,
|
I can not say what's happening here... But i can say that's the combination between Django v4.2.16 and pillow '10.4.0' vs. '11.0.0' >>> import django
>>> django.__version__
'4.2.16'
>>> Image.open((x.thumbnail.open('rb'))).width
4000
>>> import PIL
>>> PIL.__version__
'11.0.0'
>>> x.thumbnail.width
/root/.local/share/virtualenvs/foobar/lib/python3.11/site-packages/PIL/TiffImagePlugin.py:935: UserWarning: Corrupt EXIF data. Expecting to read 2 bytes but only got 0.
warnings.warn(str(msg))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/root/.local/share/virtualenvs/foobar/lib/python3.11/site-packages/django/core/files/images.py", line 20, in width
return self._get_image_dimensions()[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/share/virtualenvs/foobar/lib/python3.11/site-packages/django/core/files/images.py", line 30, in _get_image_dimensions
self._dimensions_cache = get_image_dimensions(self, close=close)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/share/virtualenvs/foobar/lib/python3.11/site-packages/django/core/files/images.py", line 63, in get_image_dimensions
p.feed(data)
File "/root/.local/share/virtualenvs/foobar/lib/python3.11/site-packages/PIL/ImageFile.py", line 471, in feed
im = Image.open(fp)
^^^^^^^^^^^^^^
File "/root/.local/share/virtualenvs/foobar/lib/python3.11/site-packages/PIL/Image.py", line 3515, in open
im = _open_core(fp, filename, prefix, formats)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/share/virtualenvs/foobar/lib/python3.11/site-packages/PIL/Image.py", line 3503, in _open_core
im = factory(fp, filename)
^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/share/virtualenvs/foobar/lib/python3.11/site-packages/PIL/TiffImagePlugin.py", line 1153, in __init__
super().__init__(fp, filename)
File "/root/.local/share/virtualenvs/foobar/lib/python3.11/site-packages/PIL/ImageFile.py", line 144, in __init__
self._open()
File "/root/.local/share/virtualenvs/foobar/lib/python3.11/site-packages/PIL/TiffImagePlugin.py", line 1177, in _open
self._seek(0)
File "/root/.local/share/virtualenvs/foobar/lib/python3.11/site-packages/PIL/TiffImagePlugin.py", line 1248, in _seek
self._setup()
File "/root/.local/share/virtualenvs/foobar/lib/python3.11/site-packages/PIL/TiffImagePlugin.py", line 1426, in _setup
raise ValueError(msg)
ValueError: Invalid dimensions
>>> Think it's related to the way how Django tries to get the image size: https://github.com/django/django/blob/968397228fe03968bb855856532569586c8a8a1c/django/core/files/images.py#L35-L89 |
Yes, it's the combination of how Django tries to get the image size via xsize = self.tag_v2.get(IMAGEWIDTH)
ysize = self.tag_v2.get(IMAGELENGTH)
if not isinstance(xsize, int) or not isinstance(ysize, int):
msg = "Invalid dimensions"
raise ValueError(msg) |
Hello 👋 Django Fellow here In Django we are using Pillow as documented to parse an image: https://pillow.readthedocs.io/en/stable/reference/ImageFile.html#example-parse-an-image Using this this file python-logo.tiff and following the example from PIL import ImageFile
fp = open("python-logo.tiff", "rb")
p = ImageFile.Parser()
while 1:
s = fp.read(1024)
if not s:
break
p.feed(s)
im = p.close()
im.save("copy.tiff") This raises the
No error is raised on Pillow 10.4.0 We can add new error handling in Django but before doing so, I would like to confirm that this is not an issue in Pillow 11 and that the documented example is missing error handling also |
Hi. Thanks for breaking the problem down. The code change that led to this wasn't intended to change behaviour, and your use case is reasonable. I've created #8535 to resolve this. |
Seems that v11.0.0 release add a Bug parsing tiff images:
more info in Sentry message:
Runtime: CPython v3.11.10(3.11.10 (main, Oct 19 2024, 01:04:28) [GCC 12.2.0])
It works with Pillow 10.4.0
The text was updated successfully, but these errors were encountered: