Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#233 closed (invalid)

Using ImageField without PIL

Reported by: nesh <nesh [at] studioquattro [dot] co [dot] yu> Owned by: adrian
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:

Description

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 http://draco.boskant.nl/:

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 = fhandle.read(24)
    if len(head) != 24:
        return
    if head[:4] == '\x89PNG':
        # PNG
        check = struct.unpack('>i', head[4:8])[0]
        if check != 0x0d0a1a0a:
            return
        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'
        try:
            fhandle.seek(0)  # Read 0xff next
            size = 2
            ftype = 0
            while not 0xc0 <= ftype <= 0xcf:
                fhandle.seek(size, 1)
                byte = fhandle.read(1)
                while ord(byte) == 0xff: 
                    byte = fhandle.read(1)
                ftype = ord(byte)
                size = struct.unpack('>H', fhandle.read(2))[0] - 2
            # We are at a SOFn block
            fhandle.seek(1, 1)  # Skip `precision' byte.
            height, width = struct.unpack('>HH', fhandle.read(4))
        except Exception: #IGNORE:W0703
            return
    else:
        return
    return img_type, width, height
# imageSize

Change History (3)

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

  • Resolution set to invalid
  • Status changed from new to closed

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 9 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 http://djangoutils.python-hosting.com/wiki/Thumbnails

comment:3 Changed 9 years ago by URL

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