Pillow/Tests/test_file_ppm.py

57 lines
1.5 KiB
Python
Raw Normal View History

2014-07-07 21:03:50 +04:00
from helper import unittest, PillowTestCase
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):
2014-06-10 13:10:47 +04:00
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")
2014-06-10 13:10:47 +04:00
def test_16bit_pgm(self):
im = Image.open('Tests/images/16_bit_binary.pgm')
im.load()
self.assertEqual(im.mode, 'I')
self.assertEqual(im.size, (20, 100))
2014-04-08 09:22:42 +04:00
2014-06-10 13:10:47 +04:00
tgt = Image.open('Tests/images/16_bit_binary_pgm.png')
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):
im = Image.open('Tests/images/16_bit_binary.pgm')
im.load()
2014-04-08 09:22:42 +04:00
2014-06-10 13:10:47 +04: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
2015-08-28 19:05:08 +03:00
def test_truncated_file(self):
path = self.tempfile('temp.pgm')
2016-12-28 01:54:10 +03:00
with open(path, 'w') as f:
f.write('P6')
2015-08-28 19:05:08 +03:00
self.assertRaises(ValueError, lambda: Image.open(path))
2014-06-10 13:10:47 +04: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):
Image.open('Tests/images/negative_size.ppm')
2014-06-10 13:10:47 +04:00
if __name__ == '__main__':
unittest.main()