Python PIL - Draw Circle [duplicate]
Asked Answered
O

2

26

I am trying to draw a simple circle and save this to a file using the Python Imaging Library:

import Image, ImageDraw

image = Image.new('RGBA', (200, 200))
draw = ImageDraw.Draw(image)
draw.ellipse((20, 180, 180, 20), fill = 'blue', outline ='blue')
draw.point((100, 100), 'red')
image.save('test.png')

The point draw.point appears on the image, but the ellipse itself does not. I tried changing the mode to just RGB (I thought the mode might affect what is displayed), but this did not solve it.

How can I fix this? Thanks!

Ontario answered 23/12, 2013 at 16:26 Comment(0)
S
33

Instead of specifying the upper right and lower left coordinates, swap them to get the upper left and lower right.

draw.ellipse((20, 20, 180, 180), fill = 'blue', outline ='blue')
Stake answered 23/12, 2013 at 16:36 Comment(3)
Ah, I didn't think there would be a difference. Thanks!Ontario
@F3AR3DLEGEND, the documentation is pretty quiet on the subject isn't it? All it mentions is a bounding box, and you'd think any corners of the box would do.Stake
Yup. I thought any pair of opposite corners would work, but apparently not.Ontario
O
11

Your ellipsis coordinates are incorrect, that should be (x1, y1, x2, y2), where x1 <= x2 and y1 <= y2, as those pairs, (x1, y1) and (x2, y2), represents respectively top left and bottom right corners of enclosing rectangle.

Try to change to

draw.ellipse((20, 20, 180, 180), fill = 'blue', outline ='blue')

enter image description here

Oldtime answered 23/12, 2013 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.