1
0
mirror of https://github.com/sashacmc/photo-importer.git synced 2025-02-16 18:34:34 +02:00

Update rotator to new PyExifTool

This commit is contained in:
Alexander Bushnev 2022-11-29 21:31:17 +01:00
parent bd7c062c0a
commit 7ee80652d2

View File

@ -7,7 +7,7 @@ import tempfile
import subprocess
import concurrent.futures
from photo_importer import config
from . import config
JPEGTRAN_COMMAND = {
0: None,
@ -39,8 +39,7 @@ class Rotator(object):
processor = self.__process_exiftran
self.__exiftool = None
if int(self.__config['main']['use_jpegtran']):
self.__exiftool = exiftool.ExifTool()
self.__exiftool.start()
self.__exiftool = exiftool.ExifToolHelper()
processor = self.__process_jpegtran
tc = 1
@ -69,25 +68,24 @@ class Rotator(object):
if self.__dryrun:
return True
p = subprocess.Popen(
error = ''
with subprocess.Popen(
cmd,
shell=True,
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
).stderr
) as p:
while True:
line = p.stderr.readline()
if not line:
break
error = ''
while True:
line = p.readline()
if not line:
break
if line.startswith('processing '):
ok = True
else:
ok = False
error += line
if line.startswith('processing '):
ok = True
else:
ok = False
error += line
if error != '':
logging.error('exiftran (%s) error: %s' % (filename, error))
@ -119,18 +117,17 @@ class Rotator(object):
filename,
)
p = subprocess.Popen(
with subprocess.Popen(
cmd,
shell=True,
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
).stderr
line = p.readline()
if line:
logging.error('jpegtran (%s) failed: %s' % (filename, line))
return False
) as p:
line = p.stderr.readline()
if line:
logging.error('jpegtran (%s) failed: %s' % (filename, line))
return False
self.__clear_orientation_tag(tmpfile)
@ -143,22 +140,17 @@ class Rotator(object):
return False
def __get_orientation_cmd(self, fullname):
orientation = self.__exiftool.get_tag(ORIENTATION_TAG, fullname)
if (
orientation is not None
and 0 <= orientation
and orientation < len(JPEGTRAN_COMMAND)
):
tags = self.__exiftool.get_tags(fullname, ORIENTATION_TAG)
if ORIENTATION_TAG not in tags:
return None
orientation = tags[ORIENTATION_TAG]
if 0 <= orientation and orientation < len(JPEGTRAN_COMMAND):
return JPEGTRAN_COMMAND[orientation]
else:
return None
def __clear_orientation_tag(self, fullname):
res = self.__exiftool.set_tags({ORIENTATION_TAG: 1}, fullname).decode(
'utf-8'
)
if not exiftool.check_ok(res):
raise SystemError('exiftool error: ' + exiftool.format_error(res))
self.__exiftool.set_tags(fullname, {ORIENTATION_TAG: 1})
try:
os.remove(fullname + '_original')
except Exception: