From 327ea209b8dd91b60945d74e3c32edbebee63810 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Mon, 7 Apr 2014 22:23:04 -0700 Subject: [PATCH] Write support, fixes #602 --- PIL/PpmImagePlugin.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/PIL/PpmImagePlugin.py b/PIL/PpmImagePlugin.py index d7af308c2..a5f01891f 100644 --- a/PIL/PpmImagePlugin.py +++ b/PIL/PpmImagePlugin.py @@ -127,6 +127,11 @@ def _save(im, fp, filename): rawmode, head = "1;I", b"P4" 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": @@ -134,8 +139,15 @@ def _save(im, fp, filename): else: raise IOError("cannot write mode %s as PPM" % im.mode) fp.write(head + ("\n%d %d\n" % im.size).encode('ascii')) - if head != b"P4": + if head == b"P6": fp.write(b"255\n") + if head == b"P5": + if rawmode == "I": + fp.write(b"255\n") + elif rawmode == "I;16B": + 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