-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexif_fix.py
58 lines (45 loc) · 1.93 KB
/
exif_fix.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
#!/usr/bin/env python
"""
Some EDX map images saved by ESPRIT for the FEI Osiris TEM at my institution
are saved with incorrect resolutions, posing issues with some programs (notably
PowerPoint). This script iterates over all TIF images in a directory and fixes
the tags.
"""
import os
import exifread
from PIL import Image
import piexif
directory = r'STEM'
for (dirpath, dirnames, filenames) in os.walk(directory):
for filename in filenames:
if filename.endswith('.tif'):
fullname = os.sep.join([dirpath, filename])
with open(fullname, 'rb') as f:
tags = exifread.process_file(f)
keys = ["Image XResolution", "Image YResolution"]
with Image.open(fullname) as im:
try:
tags = piexif.load(fullname)
except ValueError:
continue
w,h = im.size
try:
xres = tags['0th'][piexif.ImageIFD.XResolution][0]
except KeyError:
print(f'File {fullname} did not have xres tag; skipping.')
continue
try:
yres = tags['0th'][piexif.ImageIFD.YResolution][0]
except KeyError:
print(f'File {fullname} did not have yres tag; skipping.')
continue
print(f"{fullname} (xres, yres): ({xres}, {yres})")
if not (xres > 10*yres or 10*xres < yres):
continue
sfile = fullname[:-4] + '.tif'
try:
im.save(sfile, resolution=1)
except ValueError:
print("ValueError on save.")
except OSError:
print("OSError on save.")