This repository has been archived by the owner on Mar 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exif.py
76 lines (63 loc) · 2.29 KB
/
exif.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# coding: utf-8
from PIL import Image
from PIL.ExifTags import TAGS
class ExifException(Exception):
pass
class Exif(object):
def __init__(self, fn):
if isinstance(fn, Image.Image):
self.im = fn
self.overwrite = False
elif isinstance(fn, (str, unicode)):
self.im = Image.open(fn)
self.overwrite = True
else:
raise ExifException()
self.fn = fn
self.get_exif()
def get_exif(self):
self.data = {}
info = self.im._getexif()
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
self.data[decoded] = value
return self.data
def rotate(self):
# We rotate regarding to the EXIF orientation information
if 'Orientation' in self.data.keys():
orientation = self.data['Orientation']
if orientation == 1:
# Nothing
pass
elif orientation == 2:
# Vertical Mirror
self.im = self.im.transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 3:
# Rotation 180°
self.im = self.im.transpose(Image.ROTATE_180)
elif orientation == 4:
# Horizontal Mirror
self.im = self.im.transpose(Image.FLIP_TOP_BOTTOM)
elif orientation == 5:
# Horizontal Mirror + Rotation 270°
self.im = self.im.transpose(Image.FLIP_TOP_BOTTOM).transpose(Image.ROTATE_270)
elif orientation == 6:
# Rotation 270°
self.im = self.im.transpose(Image.ROTATE_270)
elif orientation == 7:
# Vertical Mirror + Rotation 270°
self.im = self.im.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_270)
elif orientation == 8:
# Rotation 90°
self.im = self.im.transpose(Image.ROTATE_90)
# No more Orientation information
self.data['Orientation'] = 1
def delete_exif(self):
self.im.info.clear()
def clear(self, quality=100):
self.rotate()
self.delete_exif()
if self.overwrite:
self.im.save(self.fn, quality=quality)
else:
return self.im