Python PIL.ImageDraw.ImageDraw() Examples

The following are 30 code examples of PIL.ImageDraw.ImageDraw(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module PIL.ImageDraw , or try the search function .
Example #1
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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")) 
Example #2
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_shape1(self):
        # Arrange
        im = Image.new("RGB", (100, 100), "white")
        draw = ImageDraw.Draw(im)
        x0, y0 = 5, 5
        x1, y1 = 5, 50
        x2, y2 = 95, 50
        x3, y3 = 95, 5

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

        draw.shape(s, fill=1)

        # Assert
        self.assert_image_equal(
            im, Image.open("Tests/images/imagedraw_shape1.png")) 
Example #3
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_floodfill_border(self):
        # floodfill() is experimental

        # 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))

        # Act
        ImageDraw.floodfill(
            im, centre_point, ImageColor.getrgb("red"),
            border=ImageColor.getrgb("black"))

        # Assert
        self.assert_image_equal(
            im, Image.open("Tests/images/imagedraw_floodfill2.png")) 
Example #4
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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")) 
Example #5
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #6
Source File: test_font_leaks.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _test_font(self, font):
        im = Image.new('RGB', (255, 255), 'white')
        draw = ImageDraw.ImageDraw(im)
        self._test_leak(lambda: draw.text((0, 0), "some text "*1024,  # ~10k
                                          font=font, fill="black")) 
Example #7
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_polygon_kite(self):
        # Test drawing lines of different gradients (dx>dy, dy>dx) and
        # vertical (dx==0) and horizontal (dy==0) lines
        for mode in ["RGB", "L"]:
            # Arrange
            im = Image.new(mode, (W, H))
            draw = ImageDraw.Draw(im)
            expected = "Tests/images/imagedraw_polygon_kite_{}.png".format(
                mode)

            # Act
            draw.polygon(KITE_POINTS, fill="blue", outline="yellow")

            # Assert
            self.assert_image_equal(im, Image.open(expected)) 
Example #8
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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")) 
Example #9
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_big_rectangle(self):
        # Test drawing a rectangle bigger than the image
        # Arrange
        im = Image.new("RGB", (W, H))
        bbox = [(-1, -1), (W+1, H+1)]
        draw = ImageDraw.Draw(im)
        expected = "Tests/images/imagedraw_big_rectangle.png"

        # Act
        draw.rectangle(bbox, fill="orange")

        # Assert
        self.assert_image_similar(im, Image.open(expected), 1) 
Example #10
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_rectangle_width_fill(self):
        # Arrange
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)
        expected = "Tests/images/imagedraw_rectangle_width_fill.png"

        # Act
        draw.rectangle(BBOX1, fill="blue", outline="green", width=5)

        # Assert
        self.assert_image_equal(im, Image.open(expected)) 
Example #11
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_floodfill(self):
        red = ImageColor.getrgb("red")

        for mode, value in [
            ("L", 1),
            ("RGBA", (255, 0, 0, 0)),
            ("RGB", red)
        ]:
            # Arrange
            im = Image.new(mode, (W, H))
            draw = ImageDraw.Draw(im)
            draw.rectangle(BBOX2, outline="yellow", fill="green")
            centre_point = (int(W/2), int(H/2))

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

            # Assert
            expected = "Tests/images/imagedraw_floodfill_"+mode+".png"
            im_floodfill = Image.open(expected)
            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)) 
Example #12
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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")) 
Example #13
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_line_joint(self):
        im = Image.new("RGB", (500, 325))
        draw = ImageDraw.Draw(im)
        expected = "Tests/images/imagedraw_line_joint_curve.png"

        # Act
        xy = [(400, 280), (380, 280), (450, 280), (440, 120), (350, 200),
              (310, 280), (300, 280), (250, 280), (250, 200), (150, 200),
              (150, 260), (50, 200), (150, 50), (250, 100)]
        draw.line(xy, GRAY, 50, "curve")

        # Assert
        self.assert_image_similar(im, Image.open(expected), 3) 
Example #14
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_textsize_empty_string(self):
        # https://github.com/python-pillow/Pillow/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") 
Example #15
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_same_color_outline(self):
        # Prepare shape
        x0, y0 = 5, 5
        x1, y1 = 5, 50
        x2, y2 = 95, 50
        x3, y3 = 95, 5

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

        # Begin
        for mode in ["RGB", "L"]:
            for fill, outline in [
                ["red", None],
                ["red", "red"],
                ["red", "#f00"]
            ]:
                for operation, args in {
                    'chord': [BBOX1, 0, 180],
                    'ellipse': [BBOX1],
                    'shape': [s],
                    'pieslice': [BBOX1, -90, 45],
                    'polygon': [[(18, 30), (85, 30), (60, 72)]],
                    'rectangle': [BBOX1]
                }.items():
                    # Arrange
                    im = Image.new(mode, (W, H))
                    draw = ImageDraw.Draw(im)

                    # Act
                    draw_method = getattr(draw, operation)
                    args += [fill, outline]
                    draw_method(*args)

                    # Assert
                    expected = ("Tests/images/imagedraw_outline"
                                "_{}_{}.png".format(operation, mode))
                    self.assert_image_similar(im, Image.open(expected), 1) 
Example #16
Source File: watermark.py    From Python-for-Everyday-Life with MIT License 5 votes vote down vote up
def watermark_image(image_obj, watermark_text):
        # load a standard gray color
        gray_color = ImageColor.getcolor('gray', 'RGB')

        # load a font object
        font_path = (pathlib.Path('..') / 'fonts' / 'Arvo-Bold.ttf').resolve()
        font = ImageFont.truetype(str(font_path), 100)

        # the  watermark is a new image with the same size of the source image
        # and added alpha channel.
        watermark = Image.new("RGBA", image_obj.size)

        # Then we draw on that new image via an ImageDraw instance: we
        # add text wiht a specific font and a surrounding rectangle, both gray
        drawing = ImageDraw.ImageDraw(watermark, "RGBA")
        drawing.text((150, 350), watermark_text, fill=gray_color, font=font)
        drawing.rectangle([(100, 320), (1150, 500)], outline=gray_color)

        # Turn the watermark image to grayscale. Then we use an alpha filter
        # to make drawings a little bit transparent
        grayscale_watermark = watermark.convert("L")
        watermark.putalpha(grayscale_watermark)

        # We then paste the watermark on the source image
        image_obj.paste(watermark, None, watermark)

        return image_obj 
Example #17
Source File: camera.py    From honeypot-camera with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def process(self, prefix, postfix):
		now = datetime.datetime.now()
		original = Image.open(self.in_filename)
		original.thumbnail(self.size, Image.ANTIALIAS)
		original = ImageEnhance.Brightness(original).enhance(self.getDaylightIntensity(now.hour)) # overwrite original
		watermark = Image.new("RGBA", original.size)
		waterdraw = ImageDraw.ImageDraw(watermark, "RGBA")
		waterdraw.text((4, 2), "%s @ %s -- %s" % (prefix, now, postfix))
		original.paste(watermark, None, watermark)
		original.save(self.out_filename, "JPEG") 
Example #18
Source File: img.py    From multisensory with Apache License 2.0 5 votes vote down vote up
def draw_on(f, im):
  pil = to_pil(im)
  draw = ImageDraw.ImageDraw(pil)
  f(draw)
  return from_pil(pil) 
Example #19
Source File: test_models.py    From nider with MIT License 5 votes vote down vote up
def test_create_draw_object(self):
        self.img._create_image()
        self.img._create_draw_object()
        self.assertIsInstance(self.img.draw, ImageDraw.ImageDraw) 
Example #20
Source File: table.py    From code-jam-5 with MIT License 5 votes vote down vote up
def _board(self):
        board = Image.new('RGBA', (self.size, self.size), (50, 50, 50, 0))
        draw = ImageDraw.ImageDraw(board, 'RGBA')
        draw.polygon(self.apices, fill=(200, 200, 200, 0))
        draw.polygon(self.inner, fill=(78, 46, 40, 0))
        for player in self.players:
            draw.text(player.position, player.name, fill=(125, 125, 25, 0))
        return board 
Example #21
Source File: table.py    From code-jam-5 with MIT License 5 votes vote down vote up
def _turn_mark(self):
        running_bd = self.board.copy()
        draw = ImageDraw.ImageDraw(running_bd, 'RGBA')
        self.active = self.players[0]
        self.active.is_active = True
        x, y = self.active.position
        r = self.size*.1
        draw.ellipse((x-r, y-r, x+r, y+r), outline=(255, 223, 0, 0))
        for eliminated in self.eliminated:
            x, y = eliminated.position
            r = self.size*.05
            draw.ellipse((x-r, y-r, x+r, y+r), outline=(255, 0, 0, 0))
        self.players.rotate(-1)
        return running_bd 
Example #22
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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")) 
Example #23
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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))) 
Example #24
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_valueerror(self):
        im = Image.open("Tests/images/chi.gif")

        draw = ImageDraw.Draw(im)
        draw.line((0, 0), fill=(0, 0, 0)) 
Example #25
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_mode_mismatch(self):
        im = hopper("RGB").copy()

        self.assertRaises(ValueError, ImageDraw.ImageDraw, im, mode="L") 
Example #26
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #27
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_arc_no_loops(self):
        # No need to go in loops
        # Arrange
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)
        start = 5
        end = 370

        # Act
        draw.arc(BBOX1, start=start, end=end)

        # Assert
        self.assert_image_similar(
            im, Image.open("Tests/images/imagedraw_arc_no_loops.png"), 1) 
Example #28
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_arc_width(self):
        # Arrange
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)
        expected = "Tests/images/imagedraw_arc_width.png"

        # Act
        draw.arc(BBOX1, 10, 260, width=5)

        # Assert
        self.assert_image_similar(im, Image.open(expected), 1) 
Example #29
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_arc_width_fill(self):
        # Arrange
        im = Image.new("RGB", (W, H))
        draw = ImageDraw.Draw(im)
        expected = "Tests/images/imagedraw_arc_width_fill.png"

        # Act
        draw.arc(BBOX1, 10, 260, fill="yellow", width=5)

        # Assert
        self.assert_image_similar(im, Image.open(expected), 1) 
Example #30
Source File: test_imagedraw.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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"))