Ejemplo n.º 1
0
    def test_save_as_gif_animated(self):
        with open('tests/images/newtons_cradle.gif', 'rb') as f:
            image = PillowAnimatedImage.open(GIFImageFile(f))

        output = io.BytesIO()
        return_value = image.save_as_gif(output)
        output.seek(0)

        loaded_image = PillowAnimatedImage.open(GIFImageFile(output))

        self.assertTrue(loaded_image.has_animation())
        self.assertEqual(loaded_image.get_frame_count(), 34)
Ejemplo n.º 2
0
    def test_resize_animated_gif(self):
        with open('tests/images/newtons_cradle.gif', 'rb') as f:
            image = WandImage.open(GIFImageFile(f))

        resized_image = image.resize((100, 75))

        self.assertTrue(resized_image.has_animation())
Ejemplo n.º 3
0
    def test_animated_gif(self):
        with open('tests/images/newtons_cradle.gif', 'rb') as f:
            image = WandImage.open(GIFImageFile(f))

        self.assertTrue(image.has_animation())

        self.assertEqual(image.get_frame_count(), 34)
Ejemplo n.º 4
0
    def save_as_gif(self, f):
        image = self.frames[0].image
        frames = self.frames

        # All gif files use either the L or P mode but we sometimes convert them
        # to RGB/RGBA to improve the quality of resizing. We must make sure that
        # they are converted back before saving.
        if image.mode not in ['L', 'P']:
            frames = [
                frame.convert('P', palette=_PIL_Image().ADAPTIVE)
                for frame in frames
            ]

        params = {
            'save_all': True,
            'duration': image.info['duration'],
            'append_images': [frame.image for frame in frames[1:]]
        }

        if 'transparency' in image.info:
            params['transparency'] = image.info['transparency']

        image.save(f, 'GIF', **params)

        return GIFImageFile(f)
Ejemplo n.º 5
0
    def test_save_transparent_gif(self):
        with open('tests/images/transparent.gif', 'rb') as f:
            image = PillowImage.open(GIFImageFile(f))

        # Save it into memory
        f = io.BytesIO()
        image.save_as_gif(f)

        # Reload it
        f.seek(0)
        image = PillowImage.open(GIFImageFile(f))

        self.assertTrue(image.has_alpha())
        self.assertFalse(image.has_animation())

        # Check that the alpha of pixel 1,1 is 0
        self.assertEqual(image.image.convert('RGBA').getpixel((1, 1))[3], 0)
Ejemplo n.º 6
0
    def test_transparent_gif(self):
        with open('tests/images/transparent.gif', 'rb') as f:
            image = PillowImage.open(GIFImageFile(f))

        self.assertTrue(image.has_alpha())
        self.assertFalse(image.has_animation())

        # Check that the alpha of pixel 1,1 is 0
        self.assertEqual(image.image.convert('RGBA').getpixel((1, 1))[3], 0)
Ejemplo n.º 7
0
    def test_transparent_gif(self):
        with open('tests/images/transparent.gif', 'rb') as f:
            image = WandImage.open(GIFImageFile(f))

        self.assertTrue(image.has_alpha())
        self.assertFalse(image.has_animation())

        # Check that the alpha of pixel 1,1 is 0
        self.assertEqual(image.image[1][1].alpha, 0)
Ejemplo n.º 8
0
    def test_resize_transparent_gif(self):
        with open('tests/images/transparent.gif', 'rb') as f:
            image = WandImage.open(GIFImageFile(f))

        resized_image = image.resize((100, 75))

        self.assertTrue(resized_image.has_alpha())
        self.assertFalse(resized_image.has_animation())

        # Check that the alpha of pixel 1,1 is 0
        self.assertAlmostEqual(resized_image.image[1][1].alpha, 0, places=6)
Ejemplo n.º 9
0
    def test_save_as_gif_converts_back_to_supported_mode(self):
        output = io.BytesIO()

        with open('tests/images/transparent.gif', 'rb') as f:
            image = PillowImage.open(GIFImageFile(f))
            image.image = image.image.convert('RGB')

        image.save_as_gif(output)
        output.seek(0)

        image = _PIL_Image().open(output)
        self.assertEqual(image.mode, 'P')
Ejemplo n.º 10
0
    def save_as_gif(self, f):
        image = self.image

        # All gif files use either the L or P mode but we sometimes convert them
        # to RGB/RGBA to improve the quality of resizing. We must make sure that
        # they are converted back before saving.
        if image.mode not in ['L', 'P']:
            image = image.convert('P', palette=_PIL_Image().ADAPTIVE)

        if 'transparency' in image.info:
            image.save(f, 'GIF', transparency=image.info['transparency'])
        else:
            image.save(f, 'GIF')

        return GIFImageFile(f)
Ejemplo n.º 11
0
    def save_as_gif(self, f):
        with self.image.convert('gif') as converted:
            converted.save(file=f)

        return GIFImageFile(f)
Ejemplo n.º 12
0
    def test_animated_gif(self):
        with open('tests/images/newtons_cradle.gif', 'rb') as f:
            image = PillowImage.open(GIFImageFile(f))

        self.assertFalse(image.has_alpha())
        self.assertTrue(image.has_animation())