Better error message when ImageMagick does not write correct output filenames

This commit is contained in:
Kovid Goyal 2021-03-12 11:07:42 +05:30
parent 82934b84d6
commit 02d29d4816
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -118,8 +118,13 @@ class NoImageMagick(Exception):
pass
last_imagemagick_cmd: Sequence[str] = ()
def run_imagemagick(path: str, cmd: Sequence[str], keep_stdout: bool = True) -> CompletedProcess:
global last_imagemagick_cmd
import subprocess
last_imagemagick_cmd = cmd
try:
p = subprocess.run(cmd, stdout=subprocess.PIPE if keep_stdout else subprocess.DEVNULL, stderr=subprocess.PIPE)
except FileNotFoundError:
@ -222,6 +227,7 @@ def render_image(
run_imagemagick(path, cmd + [output_template])
unseen = {x.index for x in m}
for x in os.listdir(tdir):
try:
parts = x.split('.', 1)[0].split('-')
index = int(parts[-1])
unseen.discard(index)
@ -230,6 +236,8 @@ def render_image(
sz, pos = parts[3].split('+', 1)
f.canvas_width, f.canvas_height = map(positive_int, sz.split('x', 1))
f.canvas_x, f.canvas_y = map(int, pos.split('+', 1))
except Exception:
raise ValueError(f'Unexpected output filename: {x!r} produced by ImageMagick command: {last_imagemagick_cmd}')
f.path = output_prefix + f'-{index}.{m.mode}'
os.rename(os.path.join(tdir, x), f.path)
check_resize(f)