-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagefile.py
67 lines (56 loc) · 2.05 KB
/
imagefile.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
from __future__ import annotations
import os
import os
from shutil import copyfile, move
from typing import List, Callable
from ..general.mediafile import MediaFile
import datetime as dt
from PIL import Image
class ImageFile(MediaFile):
supportedjpgFormats = [".jpg", ".JPG", ".jpeg", ".JPEG"]
supportedRawFormats = [".ORF", ".NEF"]
def __init__(self, file):
"""
file must be path to a jpg-file!
"""
super().__init__(path=file, validExtensions=self.supportedjpgFormats)
if not self.isValid():
return
for rawExt in ImageFile.supportedRawFormats:
rawFileCandidate = os.path.splitext(file)[0] + rawExt
if os.path.exists(rawFileCandidate):
self.extensions.append(rawExt)
self.nrFiles += 1
def getJpg(self) -> str:
return self.pathnoext + self.extensions[0]
def getRaw(self) -> str:
if len(self.extensions) > 1:
return self.pathnoext + self.extensions[1]
return None
def markRawAsRemoved(self):
if len(self.extensions) > 1:
self.extensions.pop()
def readDateTime(self):
try:
date = None
img = Image.open(self.getJpg())
img_exif = img._getexif()
exifvalueOriginalCreation = 36867
exifValueDigitized = 36868
exifvalueChangedDate = 306
if img_exif is None or (
exifvalueOriginalCreation not in img_exif
and exifValueDigitized not in img_exif
and exifvalueChangedDate not in img_exif
):
return None
else:
if exifvalueOriginalCreation in img_exif:
date = img_exif[exifvalueOriginalCreation]
elif exifValueDigitized in img_exif:
date = img_exif[exifValueDigitized]
else:
date = img_exif[exifvalueChangedDate]
return dt.datetime.strptime(date, "%Y:%m:%d %H:%M:%S")
except:
return None