Pillow/Tests/test_mode_i16.py

110 lines
2.9 KiB
Python
Raw Normal View History

2014-07-07 21:03:50 +04:00
from helper import unittest, PillowTestCase, lena
from PIL import Image
2014-06-10 13:10:47 +04:00
class TestModeI16(PillowTestCase):
2014-06-10 13:10:47 +04:00
def verify(self, im1):
im2 = lena("I")
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
self.assertEqual(
pix1[xy], pix2[xy],
("got %r from mode %s at %s, expected %r" %
(pix1[xy], im1.mode, xy, pix2[xy])))
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.
2014-06-10 13:10:47 +04:00
def basic(mode):
2014-06-10 13:10:47 +04:00
imIn = lena("I").convert(mode)
self.verify(imIn)
2014-06-10 13:10:47 +04:00
w, h = imIn.size
2014-06-10 13:10:47 +04:00
imOut = imIn.copy()
self.verify(imOut) # copy
2014-06-10 13:10:47 +04:00
imOut = imIn.transform((w, h), Image.EXTENT, (0, 0, w, h))
self.verify(imOut) # transform
2014-06-10 13:10:47 +04:00
filename = self.tempfile("temp.im")
imIn.save(filename)
2014-06-10 13:10:47 +04:00
imOut = Image.open(filename)
2014-06-10 13:10:47 +04:00
self.verify(imIn)
self.verify(imOut)
2014-06-10 13:10:47 +04:00
imOut = imIn.crop((0, 0, w, h))
self.verify(imOut)
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))
2014-06-10 13:10:47 +04:00
self.verify(imIn)
self.verify(imOut)
2014-06-10 13:10:47 +04:00
imIn = Image.new(mode, (1, 1), 1)
self.assertEqual(imIn.getpixel((0, 0)), 1)
2014-06-10 13:10:47 +04:00
imIn.putpixel((0, 0), 2)
self.assertEqual(imIn.getpixel((0, 0)), 2)
2014-06-10 13:10:47 +04:00
if mode == "L":
max = 255
else:
max = 32767
2014-06-10 13:10:47 +04:00
imIn = Image.new(mode, (1, 1), 256)
self.assertEqual(imIn.getpixel((0, 0)), min(256, max))
2014-06-10 13:10:47 +04:00
imIn.putpixel((0, 0), 512)
self.assertEqual(imIn.getpixel((0, 0)), min(512, max))
2014-06-10 13:10:47 +04:00
basic("L")
2014-06-10 13:10:47 +04:00
basic("I;16")
basic("I;16B")
basic("I;16L")
2014-06-10 13:10:47 +04:00
basic("I")
2014-06-10 13:10:47 +04:00
def test_tobytes(self):
2014-06-10 13:10:47 +04:00
def tobytes(mode):
return Image.new(mode, (1, 1), 1).tobytes()
2014-06-10 13:10:47 +04:00
order = 1 if Image._ENDIAN == '<' else -1
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])
2014-06-10 13:10:47 +04:00
def test_convert(self):
2014-06-10 13:10:47 +04:00
im = lena("I")
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"))
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"))
2014-06-10 13:10:47 +04:00
if __name__ == '__main__':
unittest.main()
# End of file