diff --git a/Tests/test_file_palm.py b/Tests/test_file_palm.py index a6491634a..f68b5a871 100644 --- a/Tests/test_file_palm.py +++ b/Tests/test_file_palm.py @@ -46,6 +46,13 @@ class TestFilePalm(PillowTestCase): self.skipKnownBadTest("Palm P image is wrong") self.roundtrip(mode) + def test_l_ioerror(self): + # Arrange + mode = "L" + + # Act / Assert + self.assertRaises(IOError, self.helper_save_as_palm, mode) + def test_rgb_ioerror(self): # Arrange mode = "RGB" diff --git a/src/PIL/PalmImagePlugin.py b/src/PIL/PalmImagePlugin.py index 1d441271e..16b9059df 100644 --- a/src/PIL/PalmImagePlugin.py +++ b/src/PIL/PalmImagePlugin.py @@ -130,26 +130,23 @@ def _save(im, fp, filename): bpp = 8 version = 1 - elif im.mode == "L" and im.encoderinfo.get("bpp") in (1, 2, 4): + elif im.mode == "L": + if im.encoderinfo.get("bpp") in (1, 2, 4): + # this is 8-bit grayscale, so we shift it to get the high-order bits, + # and invert it because + # Palm does greyscale from white (0) to black (1) + bpp = im.encoderinfo["bpp"] + im = im.point( + lambda x, shift=8-bpp, maxval=(1 << bpp)-1: maxval - (x >> shift)) + elif im.info.get("bpp") in (1, 2, 4): + # here we assume that even though the inherent mode is 8-bit grayscale, + # only the lower bpp bits are significant. + # We invert them to match the Palm. + bpp = im.info["bpp"] + im = im.point(lambda x, maxval=(1 << bpp)-1: maxval - (x & maxval)) + else: + raise IOError("cannot write mode %s as Palm" % im.mode) - # this is 8-bit grayscale, so we shift it to get the high-order bits, - # and invert it because - # Palm does greyscale from white (0) to black (1) - bpp = im.encoderinfo["bpp"] - im = im.point( - lambda x, shift=8-bpp, maxval=(1 << bpp)-1: maxval - (x >> shift)) - # we ignore the palette here - im.mode = "P" - rawmode = "P;" + str(bpp) - version = 1 - - elif im.mode == "L" and im.info.get("bpp") in (1, 2, 4): - - # here we assume that even though the inherent mode is 8-bit grayscale, - # only the lower bpp bits are significant. - # We invert them to match the Palm. - bpp = im.info["bpp"] - im = im.point(lambda x, maxval=(1 << bpp)-1: maxval - (x & maxval)) # we ignore the palette here im.mode = "P" rawmode = "P;" + str(bpp)