moeseth
moeseth

Reputation: 1945

Draw rounded corner line in ImageDraw python

enter image description here

How to draw rounded corner line in ImageDraw?

I can draw with

draw.line((x, y, x1, y1), width=4)

But the lines corner aren't rounded and they are straight flat.

Upvotes: 4

Views: 2833

Answers (1)

Michiel Overtoom
Michiel Overtoom

Reputation: 1619

The graphics drawing primitives in PIL/Pillow are very basic and won't do nice bevels, meters, anti-aliasing and rounded edges like dedicated graphics drawing packages like pycairo (tutorial and examples).

That being said, you can emulate rounded edges on lines by drawing circles on the line ends:

from PIL import Image, ImageDraw

im = Image.new("RGB", (640, 240))
dr = ImageDraw.Draw(im)

def circle(draw, center, radius, fill):
    dr.ellipse((center[0] - radius + 1, center[1] - radius + 1, center[0] + radius - 1, center[1] + radius - 1), fill=fill, outline=None)

W = 40
COLOR = (255, 255, 255)

coords = (40, 40, 600, 200)

dr.line(coords, width=W, fill=COLOR)
circle(dr, (coords[0], coords[1]), W / 2, COLOR)
circle(dr, (coords[2], coords[3]), W / 2, COLOR)

im.show()

Upvotes: 7

Related Questions