#19457 closed Bug (fixed)

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

Reported by: melinath Owned by: bak1an
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

Attachments (0)

Change History (5)

comment:1 Changed 16 months ago by melinath

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 16 months ago by bak1an

  • Cc antonbaklanov@… added
  • Triage Stage changed from Unreviewed to Accepted

failing test
i've used your png example there

comment:3 Changed 16 months ago by bak1an

  • Owner changed from nobody to bak1an
  • Status changed from new to assigned

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 16 months ago by bak1an

  • 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

Version 0, edited 16 months ago by bak1an (next)

comment:5 Changed 16 months ago by Florian Apolloner <florian@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.