diff options
| author | Luke Plant <L.Plant.98@cantab.net> | 2010-09-10 18:45:25 +0000 |
|---|---|---|
| committer | Luke Plant <L.Plant.98@cantab.net> | 2010-09-10 18:45:25 +0000 |
| commit | 76249c39641e90cc26465fbce5530e4a77c6babe (patch) | |
| tree | 98fccb019bab0df406700dede647fec6904730fc /django | |
| parent | 443cb9b6edb227211effcbc9e1534ee04a9579af (diff) | |
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
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/files/images.py | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/django/core/files/images.py b/django/core/files/images.py index 55008b548a..228a7118c5 100644 --- a/django/core/files/images.py +++ b/django/core/files/images.py @@ -23,23 +23,26 @@ class ImageFile(File): if not hasattr(self, '_dimensions_cache'): close = self.closed self.open() - self._dimensions_cache = get_image_dimensions(self) - if close: - self.close() + self._dimensions_cache = get_image_dimensions(self, close=close) return self._dimensions_cache -def get_image_dimensions(file_or_path): - """Returns the (width, height) of an image, given an open file or a path.""" +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. + """ # 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 - + p = PILImageFile.Parser() - close = False if hasattr(file_or_path, 'read'): file = file_or_path + file_pos = file.tell() + file.seek(0) else: file = open(file_or_path, 'rb') close = True @@ -55,3 +58,5 @@ def get_image_dimensions(file_or_path): finally: if close: file.close() + else: + file.seek(file_pos) |
