Opened 6 years ago

Closed 6 years ago

Last modified 5 years ago

#19457 closed Bug (fixed)

ImageField size detection fails for some files even when PIL could succeed

Reported by: Stephen Burrows Owned by: Anton Baklanov
Component: File uploads/storage Version: 1.4
Severity: Normal Keywords: image
Cc: antonbaklanov@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Some PNG files can't have their size detected by django because the clever method of size detection Django employs (I suspect in order to be somewhat more efficient?) fails horribly in these cases.

>>> import Image
>>> with open('/path/to/file.png') as f:
...  im =
>>> im.size
(480, 362)
>>> from django.core.files.images import get_image_dimensions
>>> with open('/path/to/file.png') as f:
...  get_image_dimensions(f)
Traceback (most recent call last):
  File "<console>", line 2, in <module>
  File "django/core/files/", line 58, in get_image_dimensions
  File "PIL/", line 402, in feed
    im =
  File "PIL/", line 1965, in open
    return factory(fp, filename)
  File "PIL/", line 91, in __init__
  File "PIL/", line 331, in _open
    s =, pos, len)
  File "PIL/", line 115, in call
    return getattr(self, "chunk_" + cid)(pos, len)
  File "PIL/", line 296, in chunk_zTXt
    self.im_info[k] = self.im_text[k] = zlib.decompress(v[1:])
error: Error -5 while decompressing data: incomplete or truncated stream

Change History (7)

comment:1 Changed 6 years ago by Stephen Burrows

I can't attach the file that I'm using for testing, since it's too big (276kb), but you can download it (at least for now) here:

comment:2 Changed 6 years ago by Anton Baklanov

Cc: antonbaklanov@… added
Triage Stage: UnreviewedAccepted

failing test
i've used your png example there

comment:3 Changed 6 years ago by Anton Baklanov

Owner: changed from nobody to Anton Baklanov
Status: newassigned

this is 'fixing' problem:

@@ -50,7 +50,7 @@ def get_image_dimensions(file_or_path, close=False):
         # Most of the time PIL only needs a small chunk to parse the image and
         # get the dimensions, but with some TIFF files PIL needs to parse the
         # whole file.
-        chunk_size = 1024
+        chunk_size = 2048
         while 1:
             data =
             if not data:

i will try to dig into that and find more robust solution.

comment:4 Changed 6 years ago by Anton Baklanov

Has patch: set

pull request

maybe we also need to contact with PIL guys, because Parser.feed() does not supposed to raise zlib.error in case like our, it must silently wait for more data

Last edited 6 years ago by Anton Baklanov (previous) (diff)

comment:5 Changed 6 years ago by Florian Apolloner <florian@…>

Resolution: fixed
Status: assignedclosed

In 3aa4b8165da23a2f094d0eeffacbda5484f4c1f6:

Fixed #19457 -- ImageField size detection failed for some files.

This was caused by PIL raising a zlib truncated stream error since we fed
the parser with chunks instead of the whole image.

comment:6 Changed 5 years ago by KS Chan

May I ask that why this fix is not backported to Django v1.4 LTS?

comment:7 Changed 5 years ago by Aymeric Augustin

Only security fixes get backported.

Note: See TracTickets for help on using tickets.
Back to Top