Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#233 closed (invalid)

Using ImageField without PIL

Reported by: nesh <nesh [at] studioquattro [dot] co [dot] yu> Owned by: Adrian Holovaty
Component: Core (Other) Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Using PIL to extract width and height of the image is not necessary. For that I'm using handy function that I got from draco

Works for GIF, PNG and JPEG formats.

Also, maybe is useful to cache results of function to avoid accessing to file system frequently.

Another good idea from draco is the IMG tag rewriter which automatically adds image width and height to img tags. Can this be added to template system?

def _imageInfo(fhandle):
    Determine the image type of fhandle and return its size.
    from draco

    head =
    if len(head) != 24:
    if head[:4] == '\x89PNG':
        # PNG
        check = struct.unpack('>i', head[4:8])[0]
        if check != 0x0d0a1a0a:
        width, height = struct.unpack('>ii', head[16:24])
        img_type = 'PNG'
    elif head[:6] in ('GIF87a', 'GIF89a'):
        # GIF
        width, height = struct.unpack('<HH', head[6:10])
        img_type = 'GIF'
    elif head[:4] == '\xff\xd8\xff\xe0' and head[6:10] == 'JFIF':
        # JPEG
        img_type = 'JPEG'
    # Read 0xff next
            size = 2
            ftype = 0
            while not 0xc0 <= ftype <= 0xcf:
      , 1)
                byte =
                while ord(byte) == 0xff: 
                    byte =
                ftype = ord(byte)
                size = struct.unpack('>H',[0] - 2
            # We are at a SOFn block
  , 1)  # Skip `precision' byte.
            height, width = struct.unpack('>HH',
        except Exception: #IGNORE:W0703
    return img_type, width, height
# imageSize

Change History (3)

comment:1 Changed 12 years ago by jim-django@…

Resolution: invalid
Status: newclosed

The image template tag is a good idea, but removing the PIL dependance in favor of a piece of difficult parsing code that would have to be maintainted in the django repository is not a good idea. PIL takes care of all that, including many formats and already-debugged corner cases. It's not that big a dependency.

comment:2 Changed 12 years ago by Nebojsa Djordjevic <nesh at studioquattro dot co dot yu>

I forgot about this one, see #961.

I moved all image (and thumbnails) related stuff to

comment:3 Changed 12 years ago by URL

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