2019-02-03 18:34:53 +03:00
|
|
|
from .helper import PillowTestCase, hopper
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
2014-04-11 11:28:06 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestModeI16(PillowTestCase):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-09-23 20:52:34 +04:00
|
|
|
original = hopper().resize((32, 32)).convert('I')
|
2014-07-24 04:02:57 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def verify(self, im1):
|
2014-07-24 04:02:57 +04:00
|
|
|
im2 = self.original.copy()
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(im1.size, im2.size)
|
|
|
|
pix1 = im1.load()
|
|
|
|
pix2 = im2.load()
|
|
|
|
for y in range(im1.size[1]):
|
|
|
|
for x in range(im1.size[0]):
|
|
|
|
xy = x, y
|
2014-07-24 04:02:57 +04:00
|
|
|
p1 = pix1[xy]
|
|
|
|
p2 = pix2[xy]
|
2019-05-08 14:58:33 +03:00
|
|
|
if im1.mode[0] != "I" and im2.mode[0] == "I":
|
|
|
|
p2 //= 256
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(
|
2014-07-24 04:02:57 +04:00
|
|
|
p1, p2,
|
2014-06-10 13:10:47 +04:00
|
|
|
("got %r from mode %s at %s, expected %r" %
|
2014-07-24 04:02:57 +04:00
|
|
|
(p1, im1.mode, xy, p2)))
|
2014-04-11 11:28:06 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_basic(self):
|
|
|
|
# PIL 1.1 has limited support for 16-bit image data. Check that
|
|
|
|
# create/copy/transform and save works as expected.
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def basic(mode):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-07-24 04:02:57 +04:00
|
|
|
imIn = self.original.convert(mode)
|
2014-06-10 13:10:47 +04:00
|
|
|
self.verify(imIn)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
w, h = imIn.size
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
imOut = imIn.copy()
|
|
|
|
self.verify(imOut) # copy
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
imOut = imIn.transform((w, h), Image.EXTENT, (0, 0, w, h))
|
|
|
|
self.verify(imOut) # transform
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
filename = self.tempfile("temp.im")
|
|
|
|
imIn.save(filename)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
imOut = Image.open(filename)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.verify(imIn)
|
|
|
|
self.verify(imOut)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
imOut = imIn.crop((0, 0, w, h))
|
|
|
|
self.verify(imOut)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
imOut = Image.new(mode, (w, h), None)
|
|
|
|
imOut.paste(imIn.crop((0, 0, w//2, h)), (0, 0))
|
|
|
|
imOut.paste(imIn.crop((w//2, 0, w, h)), (w//2, 0))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.verify(imIn)
|
|
|
|
self.verify(imOut)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
imIn = Image.new(mode, (1, 1), 1)
|
|
|
|
self.assertEqual(imIn.getpixel((0, 0)), 1)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
imIn.putpixel((0, 0), 2)
|
|
|
|
self.assertEqual(imIn.getpixel((0, 0)), 2)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if mode == "L":
|
2015-04-24 11:24:52 +03:00
|
|
|
maximum = 255
|
2014-06-10 13:10:47 +04:00
|
|
|
else:
|
2015-04-24 11:24:52 +03:00
|
|
|
maximum = 32767
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
imIn = Image.new(mode, (1, 1), 256)
|
2015-04-24 11:24:52 +03:00
|
|
|
self.assertEqual(imIn.getpixel((0, 0)), min(256, maximum))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
imIn.putpixel((0, 0), 512)
|
2015-04-24 11:24:52 +03:00
|
|
|
self.assertEqual(imIn.getpixel((0, 0)), min(512, maximum))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
basic("L")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
basic("I;16")
|
|
|
|
basic("I;16B")
|
|
|
|
basic("I;16L")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
basic("I")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_tobytes(self):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def tobytes(mode):
|
|
|
|
return Image.new(mode, (1, 1), 1).tobytes()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
order = 1 if Image._ENDIAN == '<' else -1
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(tobytes("L"), b"\x01")
|
|
|
|
self.assertEqual(tobytes("I;16"), b"\x01\x00")
|
|
|
|
self.assertEqual(tobytes("I;16B"), b"\x00\x01")
|
|
|
|
self.assertEqual(tobytes("I"), b"\x01\x00\x00\x00"[::order])
|
2013-04-07 21:02:11 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_convert(self):
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-07-24 04:02:57 +04:00
|
|
|
im = self.original.copy()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.verify(im.convert("I;16"))
|
|
|
|
self.verify(im.convert("I;16").convert("L"))
|
|
|
|
self.verify(im.convert("I;16").convert("I"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.verify(im.convert("I;16B"))
|
|
|
|
self.verify(im.convert("I;16B").convert("L"))
|
|
|
|
self.verify(im.convert("I;16B").convert("I"))
|