From 45bb111f27bb3b3aab961293dc69ec8140a40b7f Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 9 Mar 2022 22:01:34 +1100 Subject: [PATCH] Maximum maxval is 65535 --- src/PIL/PpmImagePlugin.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/PIL/PpmImagePlugin.py b/src/PIL/PpmImagePlugin.py index 2401dbc497d..3df24c6faf2 100644 --- a/src/PIL/PpmImagePlugin.py +++ b/src/PIL/PpmImagePlugin.py @@ -166,26 +166,19 @@ def _save(im, fp, filename): elif im.mode == "L": rawmode, head = "L", b"P5" elif im.mode == "I": - if im.getextrema()[1] < 2**16: - rawmode, head = "I;16B", b"P5" - else: - rawmode, head = "I;32B", b"P5" - elif im.mode == "RGB": - rawmode, head = "RGB", b"P6" - elif im.mode == "RGBA": + rawmode, head = "I;16B", b"P5" + elif im.mode in ("RGB", "RGBA"): rawmode, head = "RGB", b"P6" else: raise OSError(f"cannot write mode {im.mode} as PPM") - fp.write(head + ("\n%d %d\n" % im.size).encode("ascii")) + fp.write(head + b"\n%d %d\n" % im.size) if head == b"P6": fp.write(b"255\n") elif head == b"P5": if rawmode == "L": fp.write(b"255\n") - elif rawmode == "I;16B": + else: fp.write(b"65535\n") - elif rawmode == "I;32B": - fp.write(b"2147483648\n") ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, 1))]) # ALTERNATIVE: save via builtin debug function