Pillow/Tests/test_imagesequence.py

99 lines
3.1 KiB
Python
Raw Normal View History

from PIL import Image, ImageSequence, TiffImagePlugin
from .helper import PillowTestCase, hopper
2014-06-10 13:10:47 +04:00
class TestImageSequence(PillowTestCase):
def test_sanity(self):
2015-04-24 11:24:52 +03:00
test_file = self.tempfile("temp.im")
im = hopper("RGB")
2015-04-24 11:24:52 +03:00
im.save(test_file)
2014-06-10 13:10:47 +04:00
seq = ImageSequence.Iterator(im)
2014-06-10 13:10:47 +04:00
index = 0
for frame in seq:
self.assert_image_equal(im, frame)
self.assertEqual(im.tell(), index)
index += 1
2014-06-10 13:10:47 +04:00
self.assertEqual(index, 1)
2017-09-01 14:05:40 +03:00
self.assertRaises(AttributeError, ImageSequence.Iterator, 0)
2015-07-03 08:03:25 +03:00
def test_iterator(self):
2019-06-13 18:54:46 +03:00
im = Image.open("Tests/images/multipage.tiff")
i = ImageSequence.Iterator(im)
for index in range(0, im.n_frames):
self.assertEqual(i[index], next(i))
2019-06-13 18:54:46 +03:00
self.assertRaises(IndexError, lambda: i[index + 1])
2017-09-01 14:05:40 +03:00
self.assertRaises(StopIteration, next, i)
def test_iterator_min_frame(self):
2019-06-19 09:30:10 +03:00
im = Image.open("Tests/images/hopper.psd")
i = ImageSequence.Iterator(im)
for index in range(1, im.n_frames):
self.assertEqual(i[index], next(i))
2015-12-02 08:55:50 +03:00
def _test_multipage_tiff(self):
2019-06-13 18:54:46 +03:00
im = Image.open("Tests/images/multipage.tiff")
for index, frame in enumerate(ImageSequence.Iterator(im)):
frame.load()
self.assertEqual(index, im.tell())
2019-06-13 18:54:46 +03:00
frame.convert("RGB")
def test_tiff(self):
2015-12-02 08:55:50 +03:00
self._test_multipage_tiff()
def test_libtiff(self):
codecs = dir(Image.core)
if "libtiff_encoder" not in codecs or "libtiff_decoder" not in codecs:
self.skipTest("tiff support not available")
TiffImagePlugin.READ_LIBTIFF = True
2015-12-02 08:55:50 +03:00
self._test_multipage_tiff()
TiffImagePlugin.READ_LIBTIFF = False
2016-01-27 06:04:05 +03:00
def test_consecutive(self):
2019-06-13 18:54:46 +03:00
im = Image.open("Tests/images/multipage.tiff")
2016-01-27 06:04:05 +03:00
firstFrame = None
for frame in ImageSequence.Iterator(im):
2016-08-04 09:40:12 +03:00
if firstFrame is None:
2016-01-27 06:04:05 +03:00
firstFrame = frame.copy()
for frame in ImageSequence.Iterator(im):
self.assert_image_equal(frame, firstFrame)
break
def test_palette_mmap(self):
# Using mmap in ImageFile can require to reload the palette.
2019-06-13 18:54:46 +03:00
im = Image.open("Tests/images/multipage-mmap.tiff")
color1 = im.getpalette()[0:3]
im.seek(0)
color2 = im.getpalette()[0:3]
self.assertEqual(color1, color2)
2019-06-23 00:34:39 +03:00
def test_all_frames(self):
# Test a single image
im = Image.open("Tests/images/iss634.gif")
ims = ImageSequence.all_frames(im)
self.assertEqual(len(ims), 42)
for i, im_frame in enumerate(ims):
self.assertFalse(im_frame is im)
im.seek(i)
self.assert_image_equal(im, im_frame)
# Test a series of images
ims = ImageSequence.all_frames([im, hopper(), im])
self.assertEqual(len(ims), 85)
# Test an operation
ims = ImageSequence.all_frames(im, lambda im_frame: im_frame.rotate(90))
for i, im_frame in enumerate(ims):
im.seek(i)
self.assert_image_equal(im.rotate(90), im_frame)