开发者

how to fill a part of a circle using PIL?

开发者 https://www.devze.com 2023-01-01 20:00 出处:网络
I\'m trying to use PIL for a task but the result is very dirty. What I\'m doing is trying to fill a part of a piece of a circle, as you can see on the image.

I'm trying to use PIL for a task but the result is very dirty.

What I'm doing is trying to fill a part of a piece of a circle, as you can see on the image.

Here is my code:

def gen_image(values):

    side = 568
    margin = 47
    image = Image.open(settings.MEDIA_ROOT + "/i/promo_circle.jpg")

    draw = ImageDraw.Draw(image)
    draw.ellipse((margin, margin, side-margin, side-margin), outline="white")

    center = side/2
    r = side/开发者_如何学Go2 - margin

    cnt = len(values)

    for n in xrange(cnt):
        angle = n*(360.0/cnt) - 90
        next_angle = (n+1)*(360.0/cnt) - 90
        nr = (r * values[n] / 5)

        max_r = r
        min_r = nr

        for cr in xrange(min_r*10, max_r*10):
            cr = cr/10.0
            draw.arc((side/2-cr, side/2-cr, side/2+cr, side/2+cr), angle, next_angle, fill="white")

    return image


It's been a while since I used PIL, but in various other graphics libraries, there's often an aliasing problem when drawing arcs.

Have you tried enabling anti-aliasing or drawing with thicker lines?

[Edit] Having a quick look over the PIL library, I think you're right about line width etc.

Sounds like the easiest thing to do here is to build up a polygon which covers each area. So a pair of points at each end and then a load round the middle to stop the edges looking jagged. Does that make sense?


Instead of erasing with white, consider drawing a mask of just the areas you want to show. Here's an example of this for a circular mask.

How do I generate circular thumbnails with PIL?


You draw a circle on a separate image and then remove the slice of the circle you don't want by creating a triangle over that area and removing it from the image (make it transparent). Then you copy this circle segment into the image where you want it.

If you want a partial ring, draw a circle and then draw a smaller circle to cut from the first, then use radial lines to make a triangle to remove the parts you don't need in the same way.

0

精彩评论

暂无评论...
验证码 换一张
取 消