2008-08-09 04:59:02 +08:00
|
|
|
"""
|
|
|
|
Utility functions for handling images.
|
|
|
|
|
|
|
|
Requires PIL, as you might imagine.
|
|
|
|
"""
|
|
|
|
|
2012-12-14 00:26:34 +08:00
|
|
|
import zlib
|
|
|
|
import sys
|
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
from django.core.files import File
|
2012-12-14 00:26:34 +08:00
|
|
|
from django.utils import six
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
class ImageFile(File):
|
|
|
|
"""
|
|
|
|
A mixin for use alongside django.core.files.base.File, which provides
|
|
|
|
additional features for dealing with images.
|
|
|
|
"""
|
|
|
|
def _get_width(self):
|
|
|
|
return self._get_image_dimensions()[0]
|
|
|
|
width = property(_get_width)
|
|
|
|
|
|
|
|
def _get_height(self):
|
|
|
|
return self._get_image_dimensions()[1]
|
|
|
|
height = property(_get_height)
|
|
|
|
|
|
|
|
def _get_image_dimensions(self):
|
|
|
|
if not hasattr(self, '_dimensions_cache'):
|
2009-05-11 17:57:19 +08:00
|
|
|
close = self.closed
|
|
|
|
self.open()
|
2010-09-11 02:45:25 +08:00
|
|
|
self._dimensions_cache = get_image_dimensions(self, close=close)
|
2008-08-09 04:59:02 +08:00
|
|
|
return self._dimensions_cache
|
|
|
|
|
2010-09-11 02:45:25 +08:00
|
|
|
def get_image_dimensions(file_or_path, close=False):
|
|
|
|
"""
|
|
|
|
Returns the (width, height) of an image, given an open file or a path. Set
|
|
|
|
'close' to True to close the file at the end if it is initially in an open
|
|
|
|
state.
|
|
|
|
"""
|
2010-02-15 02:28:28 +08:00
|
|
|
# Try to import PIL in either of the two ways it can end up installed.
|
|
|
|
try:
|
|
|
|
from PIL import ImageFile as PILImageFile
|
|
|
|
except ImportError:
|
|
|
|
import ImageFile as PILImageFile
|
2010-09-11 02:45:25 +08:00
|
|
|
|
2008-08-09 05:57:25 +08:00
|
|
|
p = PILImageFile.Parser()
|
2008-08-09 04:59:02 +08:00
|
|
|
if hasattr(file_or_path, 'read'):
|
|
|
|
file = file_or_path
|
2010-09-11 02:45:25 +08:00
|
|
|
file_pos = file.tell()
|
|
|
|
file.seek(0)
|
2008-08-09 04:59:02 +08:00
|
|
|
else:
|
|
|
|
file = open(file_or_path, 'rb')
|
2009-05-08 18:56:51 +08:00
|
|
|
close = True
|
|
|
|
try:
|
2012-07-31 03:54:29 +08:00
|
|
|
# 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
|
2009-05-08 18:56:51 +08:00
|
|
|
while 1:
|
2012-07-31 03:54:29 +08:00
|
|
|
data = file.read(chunk_size)
|
2009-05-08 18:56:51 +08:00
|
|
|
if not data:
|
|
|
|
break
|
2012-12-14 00:26:34 +08:00
|
|
|
try:
|
|
|
|
p.feed(data)
|
|
|
|
except zlib.error as e:
|
|
|
|
# ignore zlib complaining on truncated stream, just feed more
|
|
|
|
# data to parser (ticket #19457).
|
|
|
|
if e.message.startswith("Error -5"):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
six.reraise(*sys.exc_info())
|
2009-05-08 18:56:51 +08:00
|
|
|
if p.image:
|
|
|
|
return p.image.size
|
2012-07-31 03:54:29 +08:00
|
|
|
chunk_size = chunk_size*2
|
2009-05-08 18:56:51 +08:00
|
|
|
return None
|
|
|
|
finally:
|
|
|
|
if close:
|
|
|
|
file.close()
|
2010-09-11 02:45:25 +08:00
|
|
|
else:
|
|
|
|
file.seek(file_pos)
|