Skip to content

Commit

Permalink
Maximum maxval is 65535
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Mar 9, 2022
1 parent 60de3b7 commit 45bb111
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/PIL/PpmImagePlugin.py
Expand Up @@ -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
Expand Down

0 comments on commit 45bb111

Please sign in to comment.