Ticket #11158: patch
File patch, 1.7 KB (added by , 15 years ago) |
---|
-
images.py
21 21 22 22 def _get_image_dimensions(self): 23 23 if not hasattr(self, '_dimensions_cache'): 24 close = self.closed25 self.open()26 24 self._dimensions_cache = get_image_dimensions(self) 27 if close:28 self.close()29 25 return self._dimensions_cache 30 26 31 27 def get_image_dimensions(file_or_path): 32 28 """Returns the (width, height) of an image, given an open file or a path.""" 33 29 from PIL import ImageFile as PILImageFile 34 30 p = PILImageFile.Parser() 35 close = False 31 32 we_opened_this_file = False 33 ptr_pos = -1 36 34 if hasattr(file_or_path, 'read'): 37 35 file = file_or_path 36 ptr_pos = file.tell() 37 file.seek(0) 38 38 else: 39 we_opened_this_file = True 39 40 file = open(file_or_path, 'rb') 40 close = True 41 try: 42 while 1: 43 data = file.read(1024) 44 if not data: 45 break 46 p.feed(data) 47 if p.image: 48 return p.image.size 49 return None 50 finally: 51 if close: 52 file.close() 41 42 while 1: 43 data = file.read(1024) 44 if not data: 45 break 46 p.feed(data) 47 if p.image: 48 if we_opened_this_file: 49 pass; # perhaps it needs to be closed? 50 else: 51 file.seek(ptr_pos) 52 return p.image.size 53 54 if we_opened_this_file: 55 pass; # perhaps it needs to be closed? 56 else: 57 file.seek(ptr_pos) 58 59 return None