python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.2k stars 2.22k forks source link

Anti-aliasing with rounded_rectangle() #5577

Closed ahmed4end closed 1 year ago

ahmed4end commented 3 years ago

the rounded edges is very sharp for a squircle! is there a way to use anti aliasing in the edges?

radarhere commented 3 years ago

For the record, this was also discussed in #4765.

There isn't currently a direct way to do that. You could draw a larger image and then resize it using antialiasing.

from PIL import Image, ImageDraw
im = Image.new("RGBA", (200, 100), (0, 0, 0, 0))
draw = ImageDraw.Draw(im, "RGBA")
draw.rounded_rectangle((20, 20, 180, 80), 30, outline="#000")
im.save("normal.png")

for factor in range(2, 5):
    im = Image.new("RGBA", (factor*200, factor*100), (0, 0, 0, 0))
    draw = ImageDraw.Draw(im, "RGBA")
    draw.rounded_rectangle((factor*20, factor*20, factor*180, factor*80), factor*30, outline="#000", width=factor)
    im = im.resize((200, 100), Image.ANTIALIAS)
    im.save(str(factor)+".png")
ahmed4end commented 3 years ago

@radarhere this solution works for now, Is there any plans to implement anti-aliasing in general for the rounded rect and also the line func ?

radarhere commented 3 years ago

No active plans, no.

Nakik commented 3 years ago

@radarhere I have a question I would really love to answer That I am trying to use code:

draw = ImageDraw.Draw (im)
draw.rounded_rectangle ((20, 20, 180, 80), outline = "# 000")

I have this error: AttributeError: 'ImageDraw' object has no attribute 'rounded_rectangle' thank you very much In the end what I want to do is something like this: image

radarhere commented 3 years ago

What version of Pillow are you using?

from PIL import Image
print(Image.__version__)

rounded_rectangle() was only added in 8.2.0.

radarhere commented 1 year ago

@ahmed4end is there something new that you were after?

ahmed4end commented 1 year ago

@radarhere performance of generating a rounded rectangle that's bigger by number of times then resizing is not great on some projects

radarhere commented 1 year ago

on some projects

So it is ok on some projects? What is the difference between those?