Pillow/Tests/test_file_ppm.py

76 lines
2.5 KiB
Python
Raw Normal View History

from PIL import Image
from .helper import PillowTestCase, assert_image_equal, assert_image_similar, hopper
# sample ppm stream
2015-04-24 11:24:52 +03:00
test_file = "Tests/images/hopper.ppm"
2014-06-10 13:10:47 +04:00
class TestFilePpm(PillowTestCase):
def test_sanity(self):
2019-11-25 23:03:23 +03:00
with Image.open(test_file) as im:
im.load()
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "PPM")
self.assertEqual(im.get_format_mimetype(), "image/x-portable-pixmap")
2014-06-10 13:10:47 +04:00
def test_16bit_pgm(self):
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/16_bit_binary.pgm") as im:
im.load()
self.assertEqual(im.mode, "I")
self.assertEqual(im.size, (20, 100))
self.assertEqual(im.get_format_mimetype(), "image/x-portable-graymap")
2014-04-08 09:22:42 +04:00
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/16_bit_binary_pgm.png") as tgt:
assert_image_equal(im, tgt)
2014-04-08 09:22:42 +04:00
2014-06-10 13:10:47 +04:00
def test_16bit_pgm_write(self):
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/16_bit_binary.pgm") as im:
im.load()
2014-04-08 09:22:42 +04:00
2019-11-25 23:03:23 +03:00
f = self.tempfile("temp.pgm")
im.save(f, "PPM")
2014-04-08 09:22:42 +04:00
2019-11-25 23:03:23 +03:00
with Image.open(f) as reloaded:
assert_image_equal(im, reloaded)
2014-04-08 09:22:42 +04:00
2019-03-04 07:11:21 +03:00
def test_pnm(self):
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/hopper.pnm") as im:
assert_image_similar(im, hopper(), 0.0001)
2019-03-04 07:11:21 +03:00
2019-11-25 23:03:23 +03:00
f = self.tempfile("temp.pnm")
im.save(f)
2019-03-04 07:11:21 +03:00
2019-11-25 23:03:23 +03:00
with Image.open(f) as reloaded:
assert_image_equal(im, reloaded)
2019-03-04 07:11:21 +03:00
2015-08-28 19:05:08 +03:00
def test_truncated_file(self):
2019-06-13 18:54:11 +03:00
path = self.tempfile("temp.pgm")
with open(path, "w") as f:
f.write("P6")
2015-08-28 19:05:08 +03:00
2017-09-01 14:05:40 +03:00
self.assertRaises(ValueError, Image.open, path)
2015-08-28 19:05:08 +03:00
def test_neg_ppm(self):
# Storage.c accepted negative values for xsize, ysize. the
# internal open_ppm function didn't check for sanity but it
# has been removed. The default opener doesn't accept negative
2016-12-28 01:54:10 +03:00
# sizes.
with self.assertRaises(IOError):
2019-06-13 18:54:11 +03:00
Image.open("Tests/images/negative_size.ppm")
2019-03-04 10:17:12 +03:00
def test_mimetypes(self):
2019-06-13 18:54:11 +03:00
path = self.tempfile("temp.pgm")
2019-03-04 10:17:12 +03:00
2019-06-13 18:54:11 +03:00
with open(path, "w") as f:
2019-03-04 10:17:12 +03:00
f.write("P4\n128 128\n255")
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(path) as im:
self.assertEqual(im.get_format_mimetype(), "image/x-portable-bitmap")
2019-03-04 10:17:12 +03:00
2019-06-13 18:54:11 +03:00
with open(path, "w") as f:
2019-03-04 10:17:12 +03:00
f.write("PyCMYK\n128 128\n255")
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(path) as im:
self.assertEqual(im.get_format_mimetype(), "image/x-portable-anymap")