2014-07-07 21:03:50 +04:00
|
|
|
from helper import unittest, PillowTestCase
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
# sample ppm stream
|
2015-04-24 11:24:52 +03:00
|
|
|
test_file = "Tests/images/hopper.ppm"
|
|
|
|
data = open(test_file, "rb").read()
|
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):
|
2014-04-08 09:10:45 +04:00
|
|
|
|
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-04-08 09:10:45 +04:00
|
|
|
|
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')
|
|
|
|
f = open(path, 'w')
|
|
|
|
f.write('P6')
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, lambda: Image.open(path))
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
# End of file
|