예제 #1
0
    def test_similar(self):
        text = 'EmbeddedBitmap'
        font_outline = ImageFont.truetype(font='Tests/fonts/DejaVuSans.ttf',
                                          size=24)
        font_bitmap = ImageFont.truetype(
            font='Tests/fonts/DejaVuSans-bitmap.ttf', size=24)
        size_outline = font_outline.getsize(text)
        size_bitmap = font_bitmap.getsize(text)
        size_final = (max(size_outline[0],
                          size_bitmap[0]), max(size_outline[1],
                                               size_bitmap[1]))
        im_bitmap = Image.new('RGB', size_final, (255, 255, 255))
        im_outline = im_bitmap.copy()
        draw_bitmap = ImageDraw.Draw(im_bitmap)
        draw_outline = ImageDraw.Draw(im_outline)

        # Metrics are different on the bitmap and ttf fonts,
        # more so on some platforms and versions of freetype than others.
        # Mac has a 1px difference, linux doesn't.
        draw_bitmap.text((0, size_final[1] - size_bitmap[1]),
                         text,
                         fill=(0, 0, 0),
                         font=font_bitmap)
        draw_outline.text((0, size_final[1] - size_outline[1]),
                          text,
                          fill=(0, 0, 0),
                          font=font_outline)
        self.assert_image_similar(im_bitmap, im_outline, 20)
예제 #2
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)
예제 #3
0
    def test_sanity(self):
        im = hopper("RGB").copy()

        draw = ImageDraw.ImageDraw(im)
        draw = ImageDraw.Draw(im)

        draw.ellipse(list(range(4)))
        draw.line(list(range(10)))
        draw.polygon(list(range(100)))
        draw.rectangle(list(range(4)))
예제 #4
0
    def test_floodfill(self):
        # Arrange
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)
        draw.rectangle(BBOX2, outline="yellow", fill="green")
        centre_point = (int(W / 2), int(H / 2))
        red = ImageColor.getrgb("red")
        im_floodfill = Image.open("Tests/images/imagedraw_floodfill.png")

        # Act
        ImageDraw.floodfill(im, centre_point, red)

        # Assert
        self.assert_image_equal(im, im_floodfill)

        # Test that using the same colour does not change the image
        ImageDraw.floodfill(im, centre_point, red)
        self.assert_image_equal(im, im_floodfill)

        # Test that filling outside the image does not change the image
        ImageDraw.floodfill(im, (W, H), red)
        self.assert_image_equal(im, im_floodfill)

        # Test filling at the edge of an image
        im = Image.new("RGB", (1, 1))
        ImageDraw.floodfill(im, (0, 0), red)
        self.assert_image_equal(im, Image.new("RGB", (1, 1), red))
예제 #5
0
 def _test_high_characters(self, message):
     tempname = self.save_font()
     font = ImageFont.load(tempname)
     im = Image.new("L", (750, 30), "white")
     draw = ImageDraw.Draw(im)
     draw.text((0, 0), message, "black", font=font)
     with Image.open('Tests/images/high_ascii_chars.png') as target:
         self.assert_image_similar(im, target, 0)
예제 #6
0
    def test_multiline_width(self):
        ttf = self.get_font()
        im = Image.new(mode='RGB', size=(300, 100))
        draw = ImageDraw.Draw(im)

        self.assertEqual(
            draw.textsize("longest line", font=ttf)[0],
            draw.multiline_textsize("longest line\nline", font=ttf)[0])
예제 #7
0
 def test_draw(self):
     tempname = self.save_font()
     font = ImageFont.load(tempname)
     im = Image.new("L", (130, 30), "white")
     draw = ImageDraw.Draw(im)
     draw.text((0, 0), message, 'black', font=font)
     with Image.open('Tests/images/test_draw_pbm_target.png') as target:
         self.assert_image_similar(im, target, 0)
예제 #8
0
    def test_floodfill_thresh(self):
        # floodfill() is experimental

        # Arrange
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)
        draw.rectangle(BBOX2, outline="darkgreen", fill="green")
        centre_point = (int(W / 2), int(H / 2))

        # Act
        ImageDraw.floodfill(im,
                            centre_point,
                            ImageColor.getrgb("red"),
                            thresh=30)

        # Assert
        self.assert_image_equal(
            im, Image.open("Tests/images/imagedraw_floodfill2.png"))
예제 #9
0
 def test_render_empty(self):
     # issue 2666
     font = self.get_font()
     im = Image.new(mode='RGB', size=(300, 100))
     target = im.copy()
     draw = ImageDraw.Draw(im)
     # should not crash here.
     draw.text((10, 10), '', font=font)
     self.assert_image_equal(im, target)
예제 #10
0
    def helper_polygon(self, points):
        # Arrange
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)

        # Act
        draw.polygon(points, fill="red", outline="blue")

        # Assert
        self.assert_image_equal(
            im, Image.open("Tests/images/imagedraw_polygon.png"))
예제 #11
0
    def helper_chord(self, mode, bbox, start, end):
        # Arrange
        im = Image.new(mode, (W, H))
        draw = ImageDraw.Draw(im)
        expected = "Tests/images/imagedraw_chord_{}.png".format(mode)

        # Act
        draw.chord(bbox, start, end, fill="red", outline="yellow")

        # Assert
        self.assert_image_similar(im, Image.open(expected), 1)
예제 #12
0
    def test_complex_unicode_text(self):
        ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

        im = Image.new(mode='RGB', size=(300, 100))
        draw = ImageDraw.Draw(im)
        draw.text((0, 0), 'السلام عليكم', font=ttf, fill=500)

        target = 'Tests/images/test_complex_unicode_text.png'
        target_img = Image.open(target)

        self.assert_image_similar(im, target_img, .5)
예제 #13
0
    def helper_ellipse(self, mode, bbox):
        # Arrange
        im = Image.new(mode, (W, H))
        draw = ImageDraw.Draw(im)
        expected = "Tests/images/imagedraw_ellipse_{}.png".format(mode)

        # Act
        draw.ellipse(bbox, fill="green", outline="blue")

        # Assert
        self.assert_image_similar(im, Image.open(expected), 1)
예제 #14
0
    def test_ellipse_edge(self):
        # Arrange
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)

        # Act
        draw.ellipse(((0, 0), (W - 1, H)), fill="white")

        # Assert
        self.assert_image_similar(
            im, Image.open("Tests/images/imagedraw_ellipse_edge.png"), 1)
예제 #15
0
    def test_unknown_align(self):
        im = Image.new(mode='RGB', size=(300, 100))
        draw = ImageDraw.Draw(im)
        ttf = self.get_font()

        # Act/Assert
        self.assertRaises(ValueError,
                          draw.multiline_text, (0, 0),
                          TEST_TEXT,
                          font=ttf,
                          align="unknown")
예제 #16
0
    def helper_point(self, points):
        # Arrange
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)

        # Act
        draw.point(points, fill="yellow")

        # Assert
        self.assert_image_equal(im,
                                Image.open("Tests/images/imagedraw_point.png"))
예제 #17
0
    def helper_pieslice(self, bbox, start, end):
        # Arrange
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)

        # Act
        draw.pieslice(bbox, start, end, fill="white", outline="blue")

        # Assert
        self.assert_image_similar(
            im, Image.open("Tests/images/imagedraw_pieslice.png"), 1)
예제 #18
0
    def test_shape2(self):
        # Arrange
        im = Image.new("RGB", (100, 100), "white")
        draw = ImageDraw.Draw(im)
        x0, y0 = 95, 95
        x1, y1 = 95, 50
        x2, y2 = 5, 50
        x3, y3 = 5, 95

        # Act
        s = ImageDraw.Outline()
        s.move(x0, y0)
        s.curve(x1, y1, x2, y2, x3, y3)
        s.line(x0, y0)

        draw.shape(s, outline="blue")

        # Assert
        self.assert_image_equal(
            im, Image.open("Tests/images/imagedraw_shape2.png"))
예제 #19
0
    def helper_rectangle(self, bbox):
        # Arrange
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)

        # Act
        draw.rectangle(bbox, fill="black", outline="green")

        # Assert
        self.assert_image_equal(
            im, Image.open("Tests/images/imagedraw_rectangle.png"))
예제 #20
0
 def create_base_image_draw(self,
                            size,
                            mode=DEFAULT_MODE,
                            background1=WHITE,
                            background2=GRAY):
     img = Image.new(mode, size, background1)
     for x in range(0, size[0]):
         for y in range(0, size[1]):
             if (x + y) % 2 == 0:
                 img.putpixel((x, y), background2)
     return (img, ImageDraw.Draw(img))
예제 #21
0
    def test_text_direction_rtl(self):
        ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

        im = Image.new(mode='RGB', size=(300, 100))
        draw = ImageDraw.Draw(im)
        draw.text((0, 0), 'English عربي', font=ttf, fill=500, direction='rtl')

        target = 'Tests/images/test_direction_rtl.png'
        target_img = Image.open(target)

        self.assert_image_similar(im, target_img, .5)
예제 #22
0
    def test_kerning_features(self):
        ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)

        im = Image.new(mode='RGB', size=(300, 100))
        draw = ImageDraw.Draw(im)
        draw.text((0, 0), 'TeToAV', font=ttf, fill=500, features=['-kern'])

        target = 'Tests/images/test_kerning_features.png'
        target_img = Image.open(target)

        self.assert_image_similar(im, target_img, .5)
예제 #23
0
    def test_reduce(self):
        test_color = 254

        for size in range(400000, 400010, 2):
            i = Image.new('L', (size, 1), 0)
            draw = ImageDraw.Draw(i)
            draw.rectangle((0, 0, i.size[0] // 2 - 1, 0), test_color)

            px = i.resize((5, i.size[1]), Image.BICUBIC).load()
            if px[2, 0] != test_color // 2:
                self.assertEqual(test_color // 2, px[2, 0])
예제 #24
0
    def test_y_offset(self):
        ttf = ImageFont.truetype("Tests/fonts/NotoNastaliqUrdu-Regular.ttf",
                                 FONT_SIZE)

        im = Image.new(mode='RGB', size=(300, 100))
        draw = ImageDraw.Draw(im)
        draw.text((0, 0), 'العالم العربي', font=ttf, fill=500)

        target = 'Tests/images/test_y_offset.png'
        target_img = Image.open(target)

        self.assert_image_similar(im, target_img, 1.7)
예제 #25
0
    def test_bitmap(self):
        # Arrange
        small = Image.open("Tests/images/pil123rgba.png").resize((50, 50))
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)

        # Act
        draw.bitmap((10, 10), small)

        # Assert
        self.assert_image_equal(
            im, Image.open("Tests/images/imagedraw_bitmap.png"))
예제 #26
0
    def test_textsize_empty_string(self):
        # https://github.com/python-pillow/Pillow2/issues/2783
        # Arrange
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)

        # Act
        # Should not cause 'SystemError: <built-in method getsize of
        # ImagingFont object at 0x...> returned NULL without setting an error'
        draw.textsize("")
        draw.textsize("\n")
        draw.textsize("test\n")
예제 #27
0
    def _render(self, font):
        txt = "Hello World!"
        ttf = ImageFont.truetype(font,
                                 FONT_SIZE,
                                 layout_engine=self.LAYOUT_ENGINE)
        ttf.getsize(txt)

        img = Image.new("RGB", (256, 64), "white")
        d = ImageDraw.Draw(img)
        d.text((10, 10), txt, font=ttf, fill='black')

        return img
예제 #28
0
    def test_multiline_spacing(self):
        ttf = self.get_font()

        im = Image.new(mode='RGB', size=(300, 100))
        draw = ImageDraw.Draw(im)
        draw.multiline_text((0, 0), TEST_TEXT, font=ttf, spacing=10)

        target = 'Tests/images/multiline_text_spacing.png'
        target_img = Image.open(target)

        # Epsilon ~.5 fails with FreeType 2.7
        self.assert_image_similar(im, target_img, self.metrics['multiline'])
예제 #29
0
    def helper_arc(self, bbox, start, end):
        # Arrange
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)

        # Act
        draw.arc(bbox, start, end)

        # Assert
        self.assert_image_similar(im,
                                  Image.open("Tests/images/imagedraw_arc.png"),
                                  1)
예제 #30
0
    def test_sanity(self):
        im = hopper("RGB").copy()

        draw = ImageDraw2.Draw(im)
        pen = ImageDraw2.Pen("blue", width=7)
        draw.line(list(range(10)), pen)

        from PIL2 import ImageDraw

        draw, handler = ImageDraw.getdraw(im)
        pen = ImageDraw2.Pen("blue", width=7)
        draw.line(list(range(10)), pen)