-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModel.py
More file actions
167 lines (150 loc) · 4.29 KB
/
Copy pathModel.py
File metadata and controls
167 lines (150 loc) · 4.29 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
##
## MIT License
##
## Copyright (c) 2018 Alberto Ciolini
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in all
## copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.
##
from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS
import os
import os.path
import time
from hurry.filesize import size
class MyModel:
"""
This class contains the program state. (The Model)
It provides methods to get, set, modify and evolve the state.
Attributes:
current_image current image displayed
image list of images
exif dict of exif data of current image
info dict of general info of current image
"""
def __init__(self, name=None):
"""
Init method.
Args:
current_image list of images
image current image displayed
"""
self.current_image = name
self.images = []
def update(self, name):
"""
Update the Model
:param name: name of current image
:return:
"""
self.current_image = name
def fill_list(self, image):
"""
Insert image in list
:param image: image to add to the list
:return:
"""
for i in self.images:
if i == image:
return
self.images.append(image)
def get_element(self, position):
"""
Returns element of list at certain position
:param position: position of image in list
:return:
"""
current_element = self.images[position]
self.update(current_element)
def empty_list(self):
"""
Empty the list
:return:
"""
del self.images[:]
def delete_element(self, position):
"""
Delete image from list at certain position.
if image deleted is main image update the model
:param position: position of image in list
:return:
"""
if self.images[position] == self.current_image:
self.update("")
self.images = [v for i,v in enumerate(self.images) if i != position]
def get_list(self):
"""
Return the list of images
:return: list of images
"""
return self.images
def extract_exif_data(self, image):
"""
Extract exif data of image
:param image: current image
:return:
"""
self.exif = {}
try:
img = Image.open(image)
if img.format != 'PNG':
info = img._getexif()
else:
info = img.info
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
if decoded == "GPSInfo":
gps_data = {}
for t in value:
sub_decoded = GPSTAGS.get(t, t)
gps_data[sub_decoded] = value[t]
self.exif[decoded] = gps_data
else:
self.exif[decoded] = value
except AttributeError:
print('Error with type of image')
def get_exif(self):
"""
Return the exif data
:return: exif data
"""
return self.exif
def extract_general_info(self, image):
"""
Extract general info from image
:param image: current image
:return:
"""
self.info = {}
try:
img = Image.open(image)
self.info['File name'] = os.path.basename(img.filename)
self.info['Document type'] = img.format
self.info['File size'] = size(os.stat(img.filename).st_size) + " (%5d bytes)" % os.stat(img.filename).st_size
self.info['Creation date'] = time.ctime(os.path.getctime(img.filename))
self.info['Modification date'] = time.ctime(os.path.getmtime(img.filename))
self.info['Image size'] = img.size
self.info['Color model'] = img.mode
except AttributeError:
print('Error with image')
def get_general_info(self):
"""
Return general info of image
:return: general info
"""
return self.info