Created
March 14, 2020 09:35
-
-
Save shizn/f4460b9f8ba9f546a154418f05069bde to your computer and use it in GitHub Desktop.
update exif time with file time using exiftool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import exifread | |
import os.path, time | |
import subprocess | |
import os | |
import json | |
class ExifTool(object): | |
sentinel = "{ready}\n" | |
def __init__(self, executable="/usr/local/bin/exiftool"): | |
self.executable = executable | |
def __enter__(self): | |
self.process = subprocess.Popen( | |
[self.executable, "-stay_open", "True", "-@", "-"], | |
universal_newlines=True, | |
stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
return self | |
def __exit__(self, exc_type, exc_value, traceback): | |
self.process.stdin.write("-stay_open\nFalse\n") | |
self.process.stdin.flush() | |
def execute(self, *args): | |
args = args + ("-execute\n",) | |
self.process.stdin.write(str.join("\n", args)) | |
self.process.stdin.flush() | |
output = "" | |
fd = self.process.stdout.fileno() | |
while not output.endswith(self.sentinel): | |
output += os.read(fd, 4096).decode('utf-8') | |
return output[:-len(self.sentinel)] | |
def get_metadata(self, *filenames): | |
return json.loads(self.execute("-G", "-j", "-n", *filenames)) | |
filelst = glob.glob("Documents/weixin/*.jpg") | |
# print(filelst) | |
passedfile = [] | |
processedfile = [] | |
for img in filelst: | |
with ExifTool() as e: | |
metadata = e.get_metadata(img)[0] | |
if metadata.get('EXIF:DateTimeOriginal') is not None: | |
print("PASS:" + metadata.get('EXIF:DateTimeOriginal')) | |
passedfile.append(img) | |
else: | |
print("###No DATA, start to update###") | |
filemtime = metadata.get('File:FileModifyDate') | |
e.execute("-DateTimeOriginal="+filemtime, "-filemodifydate="+filemtime,img) | |
processedfile.append(img) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment