Code

Ticket #11158: patch.diff

File patch.diff, 938 bytes (added by kua, 5 years ago)

updated patch for this bug

Line 
1Index: images.py
2===================================================================
3--- images.py   (revision 11109)
4+++ images.py   (working copy)
5@@ -28,15 +28,31 @@
6     """Returns the (width, height) of an image, given an open file or a path."""
7     from PIL import ImageFile as PILImageFile
8     p = PILImageFile.Parser()
9+
10     if hasattr(file_or_path, 'read'):
11         file = file_or_path
12+        file_pos = file.tell()
13+        file.seek(0)
14+        we_opened_this_file = False
15     else:
16         file = open(file_or_path, 'rb')
17+        we_opened_this_file = True
18+
19+    def file_cleanup():
20+        if we_opened_this_file:
21+          file.close()
22+        else:
23+          file.seek(file_pos)
24+
25+
26     while 1:
27         data = file.read(1024)
28         if not data:
29             break
30         p.feed(data)
31         if p.image:
32+            file_cleanup()
33             return p.image.size
34+
35+    file_cleanup()
36     return None