From 3da370d3bd4a04822bf0a7611a55ab7eb0beff48 Mon Sep 17 00:00:00 2001 From: Johannes Schauer Marin Rodrigues Date: Sun, 6 Feb 2022 20:33:30 +0100 Subject: [PATCH] add more information about how to ignore invalid rotation values in input images --- README.md | 9 ++++++++- src/img2pdf.py | 14 +++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4f7e487..3ccb5e2 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,10 @@ Bugs only nine different values from 1 to 9 are permitted, Anroid phones and Canon DSLR cameras produce JPEG images with the invalid value of zero. Either fix your input images with `exiftool` or similar software before - passing the JPEG to `img2pdf` or run `img2pdf` with `--rotation=ifvalid`. + passing the JPEG to `img2pdf` or run `img2pdf` with `--rotation=ifvalid` + (if you run img2pdf from the commandline) or by passing + `rotation=img2pdf.Rotation.ifvalid` as an argument to `convert()` when using + img2pdf as a library. - img2pdf uses PIL (or Pillow) to obtain image meta data and to convert the input if necessary. To prevent decompression bomb denial of service attacks, @@ -191,6 +194,10 @@ The package can also be used as a library: with open("name.pdf","wb") as f: f.write(img2pdf.convert(glob.glob("/path/to/*.jpg"))) + # ignore invalid rotation values in the input images + with open("name.pdf","wb") as f: + f.write(img2pdf.convert('test.jpg'), rotation=img2pdf.Rotation.ifvalid) + # writing to file descriptor with open("name.pdf","wb") as f1, open("test.jpg") as f2: img2pdf.convert(f2, outputstream=f1) diff --git a/src/img2pdf.py b/src/img2pdf.py index 1a3b7c9..81d5ef6 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -1277,17 +1277,25 @@ def get_imgmetadata( elif value in (2, 4, 5, 7): if rotreq == Rotation.ifvalid: logger.warning( - "Unsupported flipped rotation mode (%d)", value + "Unsupported flipped rotation mode (%d): use " + "--rotation=ifvalid or " + "rotation=img2pdf.Rotation.ifvalid to ignore", + value, ) else: raise ExifOrientationError( - "Unsupported flipped rotation mode (%d)" % value + "Unsupported flipped rotation mode (%d): use " + "--rotation=ifvalid or " + "rotation=img2pdf.Rotation.ifvalid to ignore" % value ) else: if rotreq == Rotation.ifvalid: logger.warning("Invalid rotation (%d)", value) else: - raise ExifOrientationError("Invalid rotation (%d)" % value) + raise ExifOrientationError( + "Invalid rotation (%d): use --rotation=ifvalid " + "or rotation=img2pdf.Rotation.ifvalid to ignore" % value + ) elif rotreq in (Rotation.none, Rotation["0"]): rotation = 0 elif rotreq == Rotation["90"]: