Opened 11 years ago

Closed 11 years ago

Last modified 10 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

Description

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 = Image.open(f)
... 
>>> 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/images.py", line 58, in get_image_dimensions
    p.feed(data)
  File "PIL/ImageFile.py", line 402, in feed
    im = Image.open(fp)
  File "PIL/Image.py", line 1965, in open
    return factory(fp, filename)
  File "PIL/ImageFile.py", line 91, in __init__
    self._open()
  File "PIL/PngImagePlugin.py", line 331, in _open
    s = self.png.call(cid, pos, len)
  File "PIL/PngImagePlugin.py", line 115, in call
    return getattr(self, "chunk_" + cid)(pos, len)
  File "PIL/PngImagePlugin.py", 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 by Stephen Burrows, 11 years ago

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: http://s3.mirocommunity.org.s3.amazonaws.com/tyhychi/localtv/video_thumbs/43/orig.png

comment:2 by Anton Baklanov, 11 years ago

Cc: antonbaklanov@… added
Triage Stage: UnreviewedAccepted

failing test
i've used your png example there

comment:3 by Anton Baklanov, 11 years ago

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 = file.read(chunk_size)
             if not data:

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

comment:4 by Anton Baklanov, 11 years ago

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 11 years ago by Anton Baklanov (previous) (diff)

comment:5 by Florian Apolloner <florian@…>, 11 years ago

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 by KS Chan, 10 years ago

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

comment:7 by Aymeric Augustin, 10 years ago

Only security fixes get backported.

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