-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_analysis.py
66 lines (57 loc) · 2.54 KB
/
image_analysis.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
from PIL import Image
from PIL.ExifTags import TAGS, IFD #https://exiftool.org/TagNames/EXIF.html
import subprocess, os, pathlib, datetime
def start_check(directory, file_name = None):
if file_name == None:
image = Image.open(directory)
else:
image = Image.open(directory + "\\" + file_name)
fname = pathlib.Path(image.filename) #path object
# Dictionary Creation
info_dict = {
"Directory": os.path.dirname(image.filename),
"File Name": os.path.basename(image.filename),
"Size": fname.stat().st_size,
"Creation Time": datetime.datetime.fromtimestamp(fname.stat().st_ctime).strftime("%d.%m.%Y %H:%M:%S"),
"Modification Time": datetime.datetime.fromtimestamp(fname.stat().st_mtime).strftime("%d.%m.%Y %H:%M:%S"),
# "Image Size": image.size,
# "Image Height": image.height,
# "Image Width": image.width,
"Image Format": image.format,
"Image Mode": image.mode,
"Image is Animated": getattr(image, "is_animated", False),
"Frames in Image": getattr(image, "n_frames", 1)
}
# Extracting Exif metadata
exif_info = image.getexif()
for tag, value in exif_info.items():
if tag != 59932:
info_dict["[Base] " + TAGS.get(tag, "Unknown " + str(tag))] = value
else:
pass
for tag, value in exif_info.get_ifd(IFD.Exif).items():
if tag != 59932:
info_dict["[Exif] " + TAGS.get(tag, "Unknown " + str(tag))] = value
else:
pass
for tag, value in exif_info.get_ifd(IFD.GPSInfo).items():
if tag != 59932:
info_dict["[GPSInfo] " + TAGS.get(tag, "Unknown " + str(tag))] = value
else:
pass
for tag, value in exif_info.get_ifd(IFD.IFD1).items():
if tag != 59932:
info_dict["[IFD1] " + TAGS.get(tag, "Unknown " + str(tag))] = value
else:
pass
# Extracting metadata with subprocess hachoir
exeProcess = "hachoir-metadata"
process = subprocess.Popen([exeProcess,image.filename],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
for tag in process.stdout:
line = tag.strip().split(':')
if line[0].strip().replace("- ", "") != "Metadata" :
info_dict["[Hachoir] " + line[0].strip().replace("- ", "")] = line[-1].strip()
return info_dict