add more information about how to ignore invalid rotation values in input images

This commit is contained in:
Johannes Schauer Marin Rodrigues 2022-02-06 20:33:30 +01:00
parent 6cff2931e4
commit 3da370d3bd
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1
2 changed files with 19 additions and 4 deletions

View file

@ -82,7 +82,10 @@ Bugs
only nine different values from 1 to 9 are permitted, Anroid phones and 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. Canon DSLR cameras produce JPEG images with the invalid value of zero.
Either fix your input images with `exiftool` or similar software before 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 - 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, 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: with open("name.pdf","wb") as f:
f.write(img2pdf.convert(glob.glob("/path/to/*.jpg"))) 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 # writing to file descriptor
with open("name.pdf","wb") as f1, open("test.jpg") as f2: with open("name.pdf","wb") as f1, open("test.jpg") as f2:
img2pdf.convert(f2, outputstream=f1) img2pdf.convert(f2, outputstream=f1)

View file

@ -1277,17 +1277,25 @@ def get_imgmetadata(
elif value in (2, 4, 5, 7): elif value in (2, 4, 5, 7):
if rotreq == Rotation.ifvalid: if rotreq == Rotation.ifvalid:
logger.warning( 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: else:
raise ExifOrientationError( 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: else:
if rotreq == Rotation.ifvalid: if rotreq == Rotation.ifvalid:
logger.warning("Invalid rotation (%d)", value) logger.warning("Invalid rotation (%d)", value)
else: 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"]): elif rotreq in (Rotation.none, Rotation["0"]):
rotation = 0 rotation = 0
elif rotreq == Rotation["90"]: elif rotreq == Rotation["90"]: