Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33240 closed Bug (needsinfo)

get_image_dimensions() raises ValueError on some .ico files.

Reported by: NKSM Owned by: Ath Tripathi
Component: File uploads/storage Version: 2.2
Severity: Normal Keywords: pillow
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by NKSM)

When I try to get the dimensions of the image like below:

def clean_image(self):
   image = self.cleaned_data.get("image")
   if not image:
       raise forms.ValidationError("No image!")
   else:
       w, h = get_image_dimensions(image)
       if w != 100:
           raise forms.ValidationError("The image is %i pixel wide. It's supposed to be 100px" % w)
       if h != 200:
           raise forms.ValidationError("The image is %i pixel high. It's supposed to be 200px" % h)
   return image

I get : ValueError: buffer is not large enough

Error at line: https://github.com/django/django/blob/073b7b5915fdfb89a144e81173176ee13ff92a25/django/core/files/images.py#L62

# Most of the time Pillow only needs a small chunk to parse the image
# and get the dimensions, but with some TIFF files Pillow needs to
# parse the whole file.
chunk_size = 1024
while 1:
    data = file.read(chunk_size)
    if not data:
        break
    try:
        p.feed(data) ⬅️ **HERE**
    except zlib.error as e:

Exception Location: /PIL/Image.py in frombuffer, line 2605

This works well:

from PIL import Image as PillowImage
width, height = PillowImage.open(image).size

Attachments (1)

favicon.ico (1.1 KB ) - added by NKSM 3 years ago.
File to uplad

Download all attachments as: .zip

Change History (5)

by NKSM, 3 years ago

Attachment: favicon.ico added

File to uplad

comment:1 by NKSM, 3 years ago

Description: modified (diff)

comment:2 by Ath Tripathi, 3 years ago

Owner: changed from nobody to Ath Tripathi
Status: newassigned

comment:3 by Mariusz Felisiak, 3 years ago

Keywords: pillow added
Resolution: needsinfo
Status: assignedclosed

Thanks for this report, however it looks like an issue in Pillow not in Django itself. We use ImageFile.Parser() as documented.

First, I would try to report it in the Pillow issue tracker . Please reopen the ticket if you can debug your issue and provide details about why and where Django is at fault.

comment:4 by Mariusz Felisiak, 3 years ago

Summary: get_image_dimensions: ValueError: buffer is not large enoughget_image_dimensions() raises ValueError on some .ico files.
Note: See TracTickets for help on using tickets.
Back to Top