Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 840
moeseth Avatar asked Sep 17 '25 08:09

moeseth


1 Answers

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()
like image 113
Michiel Overtoom Avatar answered Sep 19 '25 05:09

Michiel Overtoom