Ticket #11158: svn_patch.diff
File svn_patch.diff, 1.2 KB (added by , 15 years ago) |
---|
-
images.py
32 32 """Returns the (width, height) of an image, given an open file or a path.""" 33 33 from PIL import ImageFile as PILImageFile 34 34 p = PILImageFile.Parser() 35 close = False 35 36 36 if hasattr(file_or_path, 'read'): 37 37 file = file_or_path 38 file_pos = file.tell() 39 file.seek(0) 40 we_opened_this_file = False 38 41 else: 39 42 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() 43 we_opened_this_file = True 44 45 def file_cleanup(): 46 if we_opened_this_file: 47 file.close() 48 else: 49 file.seek(file_pos) 50 51 52 while 1: 53 data = file.read(1024) 54 if not data: 55 break 56 p.feed(data) 57 if p.image: 58 file_cleanup() 59 return p.image.size 60 61 file_cleanup() 62 return None