2012-10-16 00:26:38 +04:00
|
|
|
from PIL import Image
|
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from .helper import PillowTestCase, hopper
|
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
# sample ppm stream
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = "Tests/images/hopper.ppm"
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-04-08 09:10:45 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestFilePpm(PillowTestCase):
|
|
|
|
def test_sanity(self):
|
2015-04-24 11:24:52 +03:00
|
|
|
im = Image.open(test_file)
|
2014-06-10 13:10:47 +04:00
|
|
|
im.load()
|
|
|
|
self.assertEqual(im.mode, "RGB")
|
|
|
|
self.assertEqual(im.size, (128, 128))
|
|
|
|
self.assertEqual(im.format, "PPM")
|
2019-03-04 10:17:12 +03:00
|
|
|
self.assertEqual(im.get_format_mimetype(), "image/x-portable-pixmap")
|
2014-04-08 09:10:45 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_16bit_pgm(self):
|
2019-06-13 18:54:11 +03:00
|
|
|
im = Image.open("Tests/images/16_bit_binary.pgm")
|
2014-06-10 13:10:47 +04:00
|
|
|
im.load()
|
2019-06-13 18:54:11 +03:00
|
|
|
self.assertEqual(im.mode, "I")
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(im.size, (20, 100))
|
2019-03-04 10:17:12 +03:00
|
|
|
self.assertEqual(im.get_format_mimetype(), "image/x-portable-graymap")
|
2014-04-08 09:22:42 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
tgt = Image.open("Tests/images/16_bit_binary_pgm.png")
|
2014-06-10 13:10:47 +04:00
|
|
|
self.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-06-13 18:54:11 +03:00
|
|
|
im = Image.open("Tests/images/16_bit_binary.pgm")
|
2014-06-10 13:10:47 +04:00
|
|
|
im.load()
|
2014-04-08 09:22:42 +04:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
f = self.tempfile("temp.pgm")
|
|
|
|
im.save(f, "PPM")
|
2014-04-08 09:22:42 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
reloaded = Image.open(f)
|
|
|
|
self.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-06-13 18:54:11 +03:00
|
|
|
im = Image.open("Tests/images/hopper.pnm")
|
2019-03-04 07:11:21 +03:00
|
|
|
self.assert_image_similar(im, hopper(), 0.0001)
|
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
f = self.tempfile("temp.pnm")
|
2019-03-04 07:11:21 +03:00
|
|
|
im.save(f)
|
|
|
|
|
|
|
|
reloaded = Image.open(f)
|
|
|
|
self.assert_image_equal(im, reloaded)
|
|
|
|
|
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
|
|
|
|
2016-09-25 12:44:22 +03:00
|
|
|
def test_neg_ppm(self):
|
2016-10-03 13:50:25 +03:00
|
|
|
# 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.
|
|
|
|
|
2016-10-03 13:50:25 +03:00
|
|
|
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")
|