Lines 1-115
Link Here
|
1 |
--- mcomix/image_tools.py.orig 2016-02-12 18:51:58 UTC |
|
|
2 |
+++ mcomix/image_tools.py |
3 |
@@ -9,7 +9,6 @@ import gtk |
4 |
from PIL import Image |
5 |
from PIL import ImageEnhance |
6 |
from PIL import ImageOps |
7 |
-from PIL.JpegImagePlugin import _getexif |
8 |
try: |
9 |
from PIL import PILLOW_VERSION |
10 |
PIL_VERSION = ('Pillow', PILLOW_VERSION) |
11 |
@@ -51,7 +50,38 @@ assert MISSING_IMAGE_ICON |
12 |
GTK_GDK_COLOR_BLACK = gtk.gdk.color_parse('black') |
13 |
GTK_GDK_COLOR_WHITE = gtk.gdk.color_parse('white') |
14 |
|
15 |
+def _getexif(im): |
16 |
+ exif={} |
17 |
+ try: |
18 |
+ exif.update(im.getexif()) |
19 |
+ except AttributeError: |
20 |
+ pass |
21 |
+ if exif: |
22 |
+ return exif |
23 |
|
24 |
+ # Exif of PNG is still buggy in Pillow 6.0.0 |
25 |
+ try: |
26 |
+ l1,l2,size,lines=im.info.get('Raw profile type exif').splitlines() |
27 |
+ if l2!='exif': |
28 |
+ # Not valid Exif data. |
29 |
+ return {} |
30 |
+ size=int(size) |
31 |
+ data=binascii.unhexlify(''.join(lines)) |
32 |
+ if len(data)!=size: |
33 |
+ # Size not match. |
34 |
+ return {} |
35 |
+ im.info['exif']=data |
36 |
+ except: |
37 |
+ # Not valid Exif data. |
38 |
+ return {} |
39 |
+ |
40 |
+ # load Exif again |
41 |
+ try: |
42 |
+ exif.update(im.getexif()) |
43 |
+ except AttributeError: |
44 |
+ pass |
45 |
+ return exif |
46 |
+ |
47 |
def rotate_pixbuf(src, rotation): |
48 |
rotation %= 360 |
49 |
if 0 == rotation: |
50 |
@@ -300,14 +330,7 @@ def pil_to_pixbuf(im, keep_orientation=False): |
51 |
) |
52 |
if keep_orientation: |
53 |
# Keep orientation metadata. |
54 |
- orientation = None |
55 |
- exif = im.info.get('exif') |
56 |
- if exif is not None: |
57 |
- exif = _getexif(im) |
58 |
- orientation = exif.get(274, None) |
59 |
- if orientation is None: |
60 |
- # Maybe it's a PNG? Try alternative method. |
61 |
- orientation = _get_png_implied_rotation(im) |
62 |
+ orientation = _getexit(im).get(274, None) |
63 |
if orientation is not None: |
64 |
setattr(pixbuf, 'orientation', str(orientation)) |
65 |
return pixbuf |
66 |
@@ -385,39 +408,6 @@ def enhance(pixbuf, brightness=1.0, contrast=1.0, satu |
67 |
im = ImageEnhance.Sharpness(im).enhance(sharpness) |
68 |
return pil_to_pixbuf(im) |
69 |
|
70 |
-def _get_png_implied_rotation(pixbuf_or_image): |
71 |
- """Same as <get_implied_rotation> for PNG files. |
72 |
- |
73 |
- Lookup for Exif data in the tEXt chunk. |
74 |
- """ |
75 |
- if isinstance(pixbuf_or_image, gtk.gdk.Pixbuf): |
76 |
- exif = pixbuf_or_image.get_option('tEXt::Raw profile type exif') |
77 |
- elif isinstance(pixbuf_or_image, Image.Image): |
78 |
- exif = pixbuf_or_image.info.get('Raw profile type exif') |
79 |
- else: |
80 |
- raise ValueError() |
81 |
- if exif is None: |
82 |
- return None |
83 |
- exif = exif.split('\n') |
84 |
- if len(exif) < 4 or 'exif' != exif[1]: |
85 |
- # Not valid Exif data. |
86 |
- return None |
87 |
- size = int(exif[2]) |
88 |
- try: |
89 |
- data = binascii.unhexlify(''.join(exif[3:])) |
90 |
- except TypeError: |
91 |
- # Not valid hexadecimal content. |
92 |
- return None |
93 |
- if size != len(data): |
94 |
- # Sizes should match. |
95 |
- return None |
96 |
- im = namedtuple('FakeImage', 'info')({ 'exif': data }) |
97 |
- exif = _getexif(im) |
98 |
- orientation = exif.get(274, None) |
99 |
- if orientation is not None: |
100 |
- orientation = str(orientation) |
101 |
- return orientation |
102 |
- |
103 |
def get_implied_rotation(pixbuf): |
104 |
"""Return the implied rotation in degrees: 0, 90, 180, or 270. |
105 |
|
106 |
@@ -429,9 +419,6 @@ def get_implied_rotation(pixbuf): |
107 |
orientation = getattr(pixbuf, 'orientation', None) |
108 |
if orientation is None: |
109 |
orientation = pixbuf.get_option('orientation') |
110 |
- if orientation is None: |
111 |
- # Maybe it's a PNG? Try alternative method. |
112 |
- orientation = _get_png_implied_rotation(pixbuf) |
113 |
if orientation == '3': |
114 |
return 180 |
115 |
elif orientation == '6': |