Fixed #11158 - get_image_dimensions very slow/incorrect after 1 call
Thanks to kua for the report, and to kua, SmileyChris and SAn for the patch git-svn-id: http://code.djangoproject.com/svn/django/trunk@13715 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
443cb9b6ed
commit
76249c3964
|
@ -23,23 +23,26 @@ class ImageFile(File):
|
||||||
if not hasattr(self, '_dimensions_cache'):
|
if not hasattr(self, '_dimensions_cache'):
|
||||||
close = self.closed
|
close = self.closed
|
||||||
self.open()
|
self.open()
|
||||||
self._dimensions_cache = get_image_dimensions(self)
|
self._dimensions_cache = get_image_dimensions(self, close=close)
|
||||||
if close:
|
|
||||||
self.close()
|
|
||||||
return self._dimensions_cache
|
return self._dimensions_cache
|
||||||
|
|
||||||
def get_image_dimensions(file_or_path):
|
def get_image_dimensions(file_or_path, close=False):
|
||||||
"""Returns the (width, height) of an image, given an open file or a path."""
|
"""
|
||||||
|
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.
|
||||||
|
"""
|
||||||
# Try to import PIL in either of the two ways it can end up installed.
|
# Try to import PIL in either of the two ways it can end up installed.
|
||||||
try:
|
try:
|
||||||
from PIL import ImageFile as PILImageFile
|
from PIL import ImageFile as PILImageFile
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import ImageFile as PILImageFile
|
import ImageFile as PILImageFile
|
||||||
|
|
||||||
p = PILImageFile.Parser()
|
p = PILImageFile.Parser()
|
||||||
close = False
|
|
||||||
if hasattr(file_or_path, 'read'):
|
if hasattr(file_or_path, 'read'):
|
||||||
file = file_or_path
|
file = file_or_path
|
||||||
|
file_pos = file.tell()
|
||||||
|
file.seek(0)
|
||||||
else:
|
else:
|
||||||
file = open(file_or_path, 'rb')
|
file = open(file_or_path, 'rb')
|
||||||
close = True
|
close = True
|
||||||
|
@ -55,3 +58,5 @@ def get_image_dimensions(file_or_path):
|
||||||
finally:
|
finally:
|
||||||
if close:
|
if close:
|
||||||
file.close()
|
file.close()
|
||||||
|
else:
|
||||||
|
file.seek(file_pos)
|
||||||
|
|
|
@ -230,3 +230,19 @@ if Image is not None:
|
||||||
finally:
|
finally:
|
||||||
del images.open
|
del images.open
|
||||||
self.assert_(FileWrapper._closed)
|
self.assert_(FileWrapper._closed)
|
||||||
|
|
||||||
|
class InconsistentGetImageDimensionsBug(TestCase):
|
||||||
|
"""
|
||||||
|
Test that get_image_dimensions() works properly after various calls using a file handler (#11158)
|
||||||
|
"""
|
||||||
|
def test_multiple_calls(self):
|
||||||
|
"""
|
||||||
|
Multiple calls of get_image_dimensions() should return the same size.
|
||||||
|
"""
|
||||||
|
from django.core.files.images import ImageFile
|
||||||
|
img_path = os.path.join(os.path.dirname(__file__), "test.png")
|
||||||
|
image = ImageFile(open(img_path))
|
||||||
|
image_pil = Image.open(img_path)
|
||||||
|
size_1, size_2 = get_image_dimensions(image), get_image_dimensions(image)
|
||||||
|
self.assertEqual(image_pil.size, size_1)
|
||||||
|
self.assertEqual(size_1, size_2)
|
||||||
|
|
Loading…
Reference in New Issue