-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Closed
Labels
Description
Feature Request
I got a feature request for the ImageDraw
module.
For more modern design, it's very useful to create a rectangle with rounded corners. Currently, it requires a higher effort to create such a rounded rectangle.
My Code:
def create_rounded_rectangle_mask(size, radius, alpha=255):
factor = 5 # Factor to increase the image size that I can later antialiaze the corners
radius = radius * factor
image = Image.new('RGBA', (size[0] * factor, size[1] * factor), (0, 0, 0, 0))
# create corner
corner = Image.new('RGBA', (radius, radius), (0, 0, 0, 0))
draw = ImageDraw.Draw(corner)
# added the fill = .. you only drew a line, no fill
draw.pieslice((0, 0, radius * 2, radius * 2), 180, 270, fill=(50, 50, 50, alpha + 55))
# max_x, max_y
mx, my = (size[0] * factor, size[1] * factor)
# paste corner rotated as needed
# use corners alpha channel as mask
image.paste(corner, (0, 0), corner)
image.paste(corner.rotate(90), (0, my - radius), corner.rotate(90))
image.paste(corner.rotate(180), (mx - radius, my - radius), corner.rotate(180))
image.paste(corner.rotate(270), (mx - radius, 0), corner.rotate(270))
# draw both inner rects
draw = ImageDraw.Draw(image)
draw.rectangle([(radius, 0), (mx - radius, my)], fill=(50, 50, 50, alpha))
draw.rectangle([(0, radius), (mx, my - radius)], fill=(50, 50, 50, alpha))
image = image.resize(size, Image.ANTIALIAS) # Smooth the corners
return image
This mask will be used to paste with a normal rectangle.
mowshon, glenn-jocher, ahiezx, dkgitdev, 1357koushik and 6 more