-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.py
74 lines (66 loc) · 2.66 KB
/
Model.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
import os
import time
import os.path
from PIL import Image
from hurry.filesize import size
from PIL.ExifTags import TAGS, GPSTAGS
class Model:
def __init__(self):
# list of the loaded images.
self.images = list()
# list of all file_names of the loaded images.
self.file_names = list()
# keep track of the current image.
self.current_image = None
# general info of the current image.
self.info = dict()
# exif data of the current image.
self.exif = dict()
# extract exif data from the current image.
def extract_exif(self, image):
img = Image.open(image)
if img._getexif():
exif_info = img._getexif().items()
self.exif = {TAGS[tag]: value for tag, value in exif_info if tag in TAGS}
if 'GPSInfo' in self.exif:
gps_data = dict()
for k in self.exif['GPSInfo'].keys():
gps_info = GPSTAGS.get(k, k)
gps_data[gps_info] = self.exif['GPSInfo'][k]
self.exif['GPSInfo'] = gps_data
else:
self.exif = dict()
return self.exif
# extract general info from the current image.
def extract_general_info(self, image):
img = Image.open(image)
if img:
self.info['FileName'] = os.path.basename(img.filename)
self.info['Format'] = img.format
self.info['FileSize'] = size(os.stat(img.filename).st_size) + " (%5d bytes)" % os.stat(
img.filename).st_size
self.info['CreationDate'] = time.ctime(os.path.getctime(img.filename))
self.info['ModificationDate'] = time.ctime(os.path.getmtime(img.filename))
self.info['ImageSize'] = img.size
self.info['ColorMode'] = img.mode
else:
self.info = dict()
return self.info
# update the current image.
def update_img(self, image):
self.current_image = image
# set the current image.
def set_current_img(self, index):
current_element = self.images[index]
self.update_img(current_element)
# delete the current image (and its file_name) and set self.current_image None if necessary.
def delete_img(self, position):
if self.images[position] == self.current_image:
self.update_img(None)
self.images.pop(position)
self.file_names.pop(position)
# set self.current_image None, empty the images and file_names list.
def empty_images_list(self):
del self.images[:]
del self.file_names[:]
self.update_img(None)