Pillow/Tests/test_file_ppm.py

76 lines
2.3 KiB
Python
Raw Normal View History

2019-03-04 07:11:21 +03:00
from .helper import PillowTestCase, hopper
from PIL import Image
# 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):
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-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
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")
im = Image.open(path)
self.assertEqual(im.get_format_mimetype(), "image/x-portable-bitmap")
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")
im = Image.open(path)
self.assertEqual(im.get_format_mimetype(), "image/x-portable-anymap")