views:

84

answers:

2

hello.

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/2 - 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
+1  A: 

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?

Jon Cage
no.. and I have no idea how to do this. sorry, I have a very little understanding about images, anti-alasing, etc. I found anti-aliasing in PIL docs here http://www.pythonware.com/library/pil/handbook/image.htm but in some resizing functions, which aren't useful for me. And I have no idea where to set line width. :(
valya
hm, there is something like line width in aggdraw module, http://effbot.org/zone/pythondoc-aggdraw.htm . but I'd really like to use only PIL since I can't simply install whatever libs I want on the server. and aggdraw module is based on PIL, so I don't think it will produce much clearer results (maybe I'm wrong)
valya
hm, yes, makes sense! thank you, I'll try it right now. gonna recall all trigonometry
valya
thank you very much, I've done it!
valya
+1  A: 

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.

http://stackoverflow.com/questions/890051/how-do-i-generate-circuar-thumbnails-with-pil

tcarobruce
wow, very interesting! thanks. Going to try it
valya