예제 #1
0
    def test_alpha_composite(self):
        # https://stackoverflow.com/questions/3374878
        # Arrange
        from PIL2 import ImageDraw

        expected_colors = sorted([
            (1122, (128, 127, 0, 255)),
            (1089, (0, 255, 0, 255)),
            (3300, (255, 0, 0, 255)),
            (1156, (170, 85, 0, 192)),
            (1122, (0, 255, 0, 128)),
            (1122, (255, 0, 0, 128)),
            (1089, (0, 255, 0, 0))])

        dst = Image.new('RGBA', size=(100, 100), color=(0, 255, 0, 255))
        draw = ImageDraw.Draw(dst)
        draw.rectangle((0, 33, 100, 66), fill=(0, 255, 0, 128))
        draw.rectangle((0, 67, 100, 100), fill=(0, 255, 0, 0))
        src = Image.new('RGBA', size=(100, 100), color=(255, 0, 0, 255))
        draw = ImageDraw.Draw(src)
        draw.rectangle((33, 0, 66, 100), fill=(255, 0, 0, 128))
        draw.rectangle((67, 0, 100, 100), fill=(255, 0, 0, 0))

        # Act
        img = Image.alpha_composite(dst, src)

        # Assert
        img_colors = sorted(img.getcolors())
        self.assertEqual(img_colors, expected_colors)
예제 #2
0
    def test_alpha_inplace(self):
        src = Image.new('RGBA', (128, 128), 'blue')

        over = Image.new('RGBA', (128, 128), 'red')
        mask = hopper('L')
        over.putalpha(mask)

        target = Image.alpha_composite(src, over)

        # basic
        full = src.copy()
        full.alpha_composite(over)
        self.assert_image_equal(full, target)

        # with offset down to right
        offset = src.copy()
        offset.alpha_composite(over, (64, 64))
        self.assert_image_equal(offset.crop((64, 64, 127, 127)),
                                target.crop((0, 0, 63, 63)))
        self.assertEqual(offset.size, (128, 128))

        # offset and crop
        box = src.copy()
        box.alpha_composite(over, (64, 64), (0, 0, 32, 32))
        self.assert_image_equal(box.crop((64, 64, 96, 96)),
                                target.crop((0, 0, 32, 32)))
        self.assert_image_equal(box.crop((96, 96, 128, 128)),
                                src.crop((0, 0, 32, 32)))
        self.assertEqual(box.size, (128, 128))

        # source point
        source = src.copy()
        source.alpha_composite(over, (32, 32), (32, 32, 96, 96))

        self.assert_image_equal(source.crop((32, 32, 96, 96)),
                                target.crop((32, 32, 96, 96)))
        self.assertEqual(source.size, (128, 128))

        # errors
        self.assertRaises(ValueError,
                          source.alpha_composite, over, "invalid source")
        self.assertRaises(ValueError,
                          source.alpha_composite, over, (0, 0),
                          "invalid destination")
        self.assertRaises(ValueError,
                          source.alpha_composite, over, (0))
        self.assertRaises(ValueError,
                          source.alpha_composite, over, (0, 0), (0))
        self.assertRaises(ValueError,
                          source.alpha_composite, over, (0, -1))
        self.assertRaises(ValueError,
                          source.alpha_composite, over, (0, 0), (0, -1))