#33240: get_image_dimensions() raises ValueError on some .ico files.
-------------------------------------+-------------------------------------
Reporter: NKSM | Owner: Ath
| Tripathi
Type: Bug | Status: new
Component: File | Version: 2.2
uploads/storage |
Severity: Normal | Resolution:
Keywords: pillow | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Matthias Kestenholz):
* resolution: needsinfo =>
* status: closed => new
Comment:
I have the same issue with Pillow 11.0.
The trouble is that Pillow's TIFF parser now raises a `ValueError` when
attempting to determine the dimensions of this image here:
https://github.com/matthiask/django-
imagefield/blob/main/tests/testapp/media/python-logo.tiff
This produces the following exception:
{{{
======================================================================
ERROR: test_websafe_versatileimageproxy
(testapp.test_imagefield.Test.test_websafe_versatileimageproxy)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/django/test/utils.py", line 446, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/django-imagefield/django-
imagefield/tests/testapp/test_imagefield.py", line 401, in
test_websafe_versatileimageproxy
m = WebsafeImage.objects.create(image="python-logo.tiff")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/django/db/models/query.py", line 658, in create
obj = self.model(**kwargs)
^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/django/db/models/base.py", line 572, in __init__
post_init.send(sender=cls, instance=self)
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/django/dispatch/dispatcher.py", line 189, in send
response = receiver(signal=self, sender=sender, **named)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/django/db/models/fields/files.py", line 519, in
update_dimension_fields
width = file.width
^^^^^^^^^^
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/django/core/files/images.py", line 21, in width
return self._get_image_dimensions()[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/django/core/files/images.py", line 31, in _get_image_dimensions
self._dimensions_cache = get_image_dimensions(self, close=close)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/django/core/files/images.py", line 64, in get_image_dimensions
p.feed(data)
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/PIL/ImageFile.py", line 471, in feed
im = Image.open(fp)
^^^^^^^^^^^^^^
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-packages/PIL/Image.py",
line 3515, in open
im = _open_core(fp, filename, prefix, formats)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-packages/PIL/Image.py",
line 3503, in _open_core
im = factory(fp, filename)
^^^^^^^^^^^^^^^^^^^^^
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/PIL/TiffImagePlugin.py", line 1153, in __init__
super().__init__(fp, filename)
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/PIL/ImageFile.py", line 144, in __init__
self._open()
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/PIL/TiffImagePlugin.py", line 1177, in _open
self._seek(0)
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/PIL/TiffImagePlugin.py", line 1248, in _seek
self._setup()
File "/home/runner/work/django-imagefield/django-
imagefield/.tox/py312-djmain/lib/python3.12/site-
packages/PIL/TiffImagePlugin.py", line 1426, in _setup
raise ValueError(msg)
ValueError: Invalid dimensions
}}}
If I modify `get_image_dimensions` to also ignore `ValueError` exceptions
and try again with a larger chunk size the tests pass.
So, it's not exactly a small self-contained example but the following
shows the breakage:
{{{
git clone
https://github.com/matthiask/django-imagefield.git
cd django-imagefield
tox -e py312-dj51
}}}
Downgrading to Pillow<11 works also, but since opening the file with
`PIL.Image` works I'd argue it's not really a Pillow issue, but an issue
with `get_image_dimensions` wanting to do interesting* things with
partially available image files. The variety of exceptions which are
ignored already show that the code tries to guard against various
problems, but it's incomplete.
I don't really have a good idea how to work around this issue since the
exception raised by Pillow is so unspecific.
*: I know the reasons and they make sense to me.
--
Ticket URL: <
https://code.djangoproject.com/ticket/33240#comment:5>