The 20+ lines of 
http://code.djangoproject.com/browser/django/trunk/django/core/files/images.py#L31
are needed to support remote storage backends.

However, quite a few of us are *not* using remote storage backends.

def get_image_dimensions(file_or_path):
    from PIL import Image
    # fallback to quick and efficient mode if operating locally
    # the RemoteStorageBase is obviously just a placeholder
    if not isinstance(file_or_path, RemoteStorageBase):
        try:
            return Image.open(file_or_path).size
        except IOError: # non-image paths raise IOErrors as well
            return None
    else:
        ... do the full dance here ...

There's a 75x difference in processing speed (*eek!*):

from PIL import Image
from PIL import ImageFile as PILImageFile
import timeit

def simple_dimensions(f):
    try:
        return Image.open(f).size
    except IOError:
        return None

def complex_dimensions(file_or_path):
    p = PILImageFile.Parser()
    close = False
    if hasattr(file_or_path, 'read'):
        file = file_or_path
    else:
        file = open(file_or_path, 'rb')
        close = True
    try:
        while 1:
            data = file.read(1024)
            if not data:
                break
            p.feed(data)
            if p.image:
                return p.image.size
        return None
    finally:
        if close:
            file.close()

print timeit.timeit('simple_dimensions("DSC00509.JPG")',
        'from __main__ import simple_dimensions', number=1000)
print timeit.timeit('complex_dimensions("DSC00509.JPG")',
        'from __main__ import complex_dimensions', number=1000)

---

$ python test_pil.py
0.159422159195
12.0412559509

---

I'd say this is a 1.1 bug.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to