Pillow/Tests/test_mode_i16.py

112 lines
3.1 KiB
Python
Raw Normal View History

from PIL import Image
from .helper import PillowTestCase, hopper
2014-06-10 13:10:47 +04:00
class TestModeI16(PillowTestCase):
2019-06-13 18:54:46 +03:00
original = hopper().resize((32, 32)).convert("I")
2014-06-10 13:10:47 +04:00
def verify(self, im1):
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
p1 = pix1[xy]
p2 = pix2[xy]
2014-06-10 13:10:47 +04:00
self.assertEqual(
2019-06-13 18:54:46 +03:00
p1,
p2,
(
"got {!r} from mode {} at {}, expected {!r}".format(
p1, im1.mode, xy, p2
)
),
2019-06-13 18:54:46 +03: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.
2014-06-10 13:10:47 +04:00
def basic(mode):
imIn = self.original.convert(mode)
2014-06-10 13:10:47 +04:00
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)
Improve handling of file resources Follow Python's file object semantics. User code is responsible for closing resources (usually through a context manager) in a deterministic way. To achieve this, remove __del__ functions. These functions used to closed open file handlers in an attempt to silence Python ResourceWarnings. However, using __del__ has the following drawbacks: - __del__ isn't called until the object's reference count reaches 0. Therefore, resource handlers remain open or in use longer than necessary. - The __del__ method isn't guaranteed to execute on system exit. See the Python documentation: https://docs.python.org/3/reference/datamodel.html#object.__del__ > It is not guaranteed that __del__() methods are called for objects > that still exist when the interpreter exits. - Exceptions that occur inside __del__ are ignored instead of raised. This has the potential of hiding bugs. This is also in the Python documentation: > Warning: Due to the precarious circumstances under which __del__() > methods are invoked, exceptions that occur during their execution > are ignored, and a warning is printed to sys.stderr instead. Instead, always close resource handlers when they are no longer in use. This will close the file handler at a specified point in the user's code and not wait until the interpreter chooses to. It is always guaranteed to run. And, if an exception occurs while closing the file handler, the bug will not be ignored. Now, when code receives a ResourceWarning, it will highlight an area that is mishandling resources. It should not simply be silenced, but fixed by closing resources with a context manager. All warnings that were emitted during tests have been cleaned up. To enable warnings, I passed the `-Wa` CLI option to Python. This exposed some mishandling of resources in ImageFile.__init__() and SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
with Image.open(filename) as imOut:
Improve handling of file resources Follow Python's file object semantics. User code is responsible for closing resources (usually through a context manager) in a deterministic way. To achieve this, remove __del__ functions. These functions used to closed open file handlers in an attempt to silence Python ResourceWarnings. However, using __del__ has the following drawbacks: - __del__ isn't called until the object's reference count reaches 0. Therefore, resource handlers remain open or in use longer than necessary. - The __del__ method isn't guaranteed to execute on system exit. See the Python documentation: https://docs.python.org/3/reference/datamodel.html#object.__del__ > It is not guaranteed that __del__() methods are called for objects > that still exist when the interpreter exits. - Exceptions that occur inside __del__ are ignored instead of raised. This has the potential of hiding bugs. This is also in the Python documentation: > Warning: Due to the precarious circumstances under which __del__() > methods are invoked, exceptions that occur during their execution > are ignored, and a warning is printed to sys.stderr instead. Instead, always close resource handlers when they are no longer in use. This will close the file handler at a specified point in the user's code and not wait until the interpreter chooses to. It is always guaranteed to run. And, if an exception occurs while closing the file handler, the bug will not be ignored. Now, when code receives a ResourceWarning, it will highlight an area that is mishandling resources. It should not simply be silenced, but fixed by closing resources with a context manager. All warnings that were emitted during tests have been cleaned up. To enable warnings, I passed the `-Wa` CLI option to Python. This exposed some mishandling of resources in ImageFile.__init__() and SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03: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)
2019-06-13 18:54:46 +03:00
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":
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
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))
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))
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):
def tobytes(mode):
return Image.new(mode, (1, 1), 1).tobytes()
2019-06-13 18:54:46 +03: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):
im = self.original.copy()
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"))