114 lines
4.1 KiB
Python
114 lines
4.1 KiB
Python
#!/usr/bin/python
|
|
|
|
from operator import contains
|
|
from PIL import Image
|
|
import piexif
|
|
import datetime, sys, os
|
|
|
|
def get_exif_attr(attrid):
|
|
items = dir(piexif.ExifIFD)
|
|
for item in items:
|
|
if not contains(item, '__'):
|
|
if attrid == piexif.ExifIFD.__dict__[item]:
|
|
return item
|
|
items = dir(piexif.ImageIFD)
|
|
for item in items:
|
|
if not contains(item, '__'):
|
|
if attrid == piexif.ImageIFD.__dict__[item]:
|
|
return item
|
|
return None
|
|
|
|
def show_exif(img_path):
|
|
img = Image.open(img_path)
|
|
exif_dict = piexif.load(img.info['exif'])
|
|
print('exif.0th:', exif_dict['0th'])
|
|
print('exif_info of %s' % img_path)
|
|
print('exif.0th:')
|
|
for k, v in exif_dict['0th'].items():
|
|
key = get_exif_attr(k)
|
|
print(key, v)
|
|
|
|
print('Exif:')
|
|
for k, v in exif_dict['Exif'].items():
|
|
key = get_exif_attr(k)
|
|
if not (key == 'MakerNote' or key == 'UserComment'):
|
|
print(key, v)
|
|
|
|
print('exif.GPS: ', exif_dict['GPS'])
|
|
print('exif.Interop: ', exif_dict['Interop'])
|
|
print('exif.1st: ', exif_dict['1st'])
|
|
|
|
def fix_exif(img_path):
|
|
img = Image.open(img_path)
|
|
exif_dict = piexif.load(img.info['exif'])
|
|
exif_dict['Exif'][piexif.ExifIFD.MakerNote] = b''
|
|
exif_dict['Exif'][piexif.ExifIFD.UserComment] = b''
|
|
# exif_dict['0th'][piexif.ImageIFD.Make] = 'Huawei'
|
|
# exif_dict['0th'][piexif.ImageIFD.Model] = 'Nexus 6P'
|
|
# exif_dict['0th'][piexif.ImageIFD.Software] = 'sux V7'
|
|
exif_dict['0th'][piexif.ImageIFD.ImageWidth] = 4056
|
|
exif_dict['0th'][piexif.ImageIFD.ImageLength] = 3040
|
|
if exif_dict['Exif'].get(piexif.ExifIFD.DigitalZoomRatio, 'havenothiskey') != 'havenothiskey':
|
|
del(exif_dict['Exif'][piexif.ExifIFD.DigitalZoomRatio])
|
|
if exif_dict['Exif'].get(piexif.ExifIFD.FocalLengthIn35mmFilm, 'havenothiskey') != 'havenothiskey':
|
|
del(exif_dict['Exif'][piexif.ExifIFD.FocalLengthIn35mmFilm])
|
|
if exif_dict['0th'].get(piexif.ImageIFD.Orientation, 'havenothiskey') != 'havenothiskey':
|
|
del(exif_dict['0th'][piexif.ImageIFD.Orientation])
|
|
exif_dict['GPS'] = {}
|
|
exif_bytes = piexif.dump(exif_dict)
|
|
img.save(img_path, exif=exif_bytes)
|
|
|
|
def fix_orientation(img_path):
|
|
img = Image.open(img_path)
|
|
exif_dict = piexif.load(img.info['exif'])
|
|
exif_dict['0th'][piexif.ImageIFD.Orientation] = 1
|
|
exif_bytes = piexif.dump(exif_dict)
|
|
img.save(img_path, exif=exif_bytes)
|
|
|
|
def fix_optimize(img_path):
|
|
img = Image.open(img_path)
|
|
img.save(img_path, optimize=True, quality=95)
|
|
|
|
def fix_rpi219(img_path):
|
|
newfile = img_path.split('.')[0] + '.' + img_path.split('.')[-1]
|
|
os.rename(img_path, newfile)
|
|
|
|
def split(path, filename):
|
|
photo1_path = path + os.sep + '.geometry'
|
|
photo2_path = path + os.sep + '.texture.TextureLayer'
|
|
if not os.path.exists(photo1_path): os.mkdir(photo1_path)
|
|
if not os.path.exists(photo2_path): os.mkdir(photo2_path)
|
|
# newfile = filename.split('_')[0] + filename[-4:]
|
|
index = filename.split('_')[-1].split('.')[0]
|
|
if index == '1':
|
|
os.rename(path + os.sep + filename, photo1_path + os.sep + filename)
|
|
print('%s -> %s' % (filename, photo1_path + os.sep + filename))
|
|
else:
|
|
os.rename(path + os.sep + filename, photo2_path + os.sep + filename)
|
|
print('%s -> %s' % (filename, photo2_path + os.sep + filename))
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) < 2:
|
|
path = '.'
|
|
else:
|
|
path = sys.argv[1]
|
|
path = os.path.abspath(path)
|
|
|
|
if os.path.isdir(path):
|
|
for file in os.listdir(path):
|
|
if file.endswith('.jpg'):
|
|
print('Fixing ' + file)
|
|
split(path, file)
|
|
# fix_rpi219(os.path.join(path, file))
|
|
# show_exif(os.path.join(path, file))
|
|
# fix_exif(path + '/' + file)
|
|
# fix_orientation(path + '/' + file)
|
|
# fix_optimize(path + '/' + file)
|
|
else:
|
|
print('Fixing ' + path)
|
|
# fix_rpi219(path)
|
|
# show_exif(path)
|
|
# fix_exif(path)
|
|
# fix_orientation(path)
|
|
# fix_optimize(path)
|