Pillow/Tests/test_file_im.py

71 lines
1.8 KiB
Python
Raw Normal View History

2015-07-03 08:03:25 +03:00
from PIL import Image, ImImagePlugin
2015-06-07 18:01:34 +03:00
from .helper import PillowTestCase, hopper
2015-06-07 18:01:34 +03:00
# sample im
TEST_IM = "Tests/images/hopper.im"
class TestFileIm(PillowTestCase):
def test_sanity(self):
im = Image.open(TEST_IM)
im.load()
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "IM")
2018-11-17 13:56:06 +03:00
def test_unclosed_file(self):
def open():
im = Image.open(TEST_IM)
im.load()
2019-06-13 18:54:11 +03:00
2018-11-17 13:56:06 +03:00
self.assert_warning(None, open)
2017-08-12 08:54:54 +03:00
def test_tell(self):
# Arrange
im = Image.open(TEST_IM)
# Act
frame = im.tell()
# Assert
self.assertEqual(frame, 0)
2015-06-07 18:01:34 +03:00
def test_n_frames(self):
im = Image.open(TEST_IM)
self.assertEqual(im.n_frames, 1)
2015-06-30 06:25:00 +03:00
self.assertFalse(im.is_animated)
2015-06-07 18:01:34 +03:00
def test_eoferror(self):
im = Image.open(TEST_IM)
n_frames = im.n_frames
2017-09-06 06:19:33 +03:00
# Test seeking past the last frame
self.assertRaises(EOFError, im.seek, n_frames)
self.assertLess(im.tell(), n_frames)
# Test that seeking to the last frame does not raise an error
2019-06-13 18:54:11 +03:00
im.seek(n_frames - 1)
2015-06-07 18:01:34 +03:00
def test_roundtrip(self):
for mode in ["RGB", "P", "PA"]:
2019-06-13 18:54:11 +03:00
out = self.tempfile("temp.im")
2017-09-01 13:36:51 +03:00
im = hopper(mode)
im.save(out)
reread = Image.open(out)
self.assert_image_equal(reread, im)
2015-06-07 18:01:34 +03:00
2017-09-01 13:36:51 +03:00
def test_save_unsupported_mode(self):
2019-06-13 18:54:11 +03:00
out = self.tempfile("temp.im")
2017-09-01 13:36:51 +03:00
im = hopper("HSV")
2017-09-01 14:05:40 +03:00
self.assertRaises(ValueError, im.save, out)
2015-06-07 18:01:34 +03:00
2015-07-03 08:03:25 +03:00
def test_invalid_file(self):
2015-07-03 09:22:56 +03:00
invalid_file = "Tests/images/flower.jpg"
2019-06-13 18:54:11 +03:00
self.assertRaises(SyntaxError, ImImagePlugin.ImImageFile, invalid_file)
2015-07-03 08:03:25 +03:00
2017-09-01 13:36:51 +03:00
def test_number(self):
self.assertEqual(1.2, ImImagePlugin.number("1.2"))