Skip to content
Advertisement

How to make a circle using PIL?

As the title says I want to draw a circle in PIL but the output isn’t a circle

The output: https://i.stack.imgur.com/qlYmD.png

Code:

  mask_im = Image.new("RGB", im2.size, (53, 108, 181))
  draw = ImageDraw.Draw(mask_im)

  x, y = mask_im.size
  eX, eY = 517, 191

  bbox = (x/2 - eX/2, y/2 - eY/2, x/2 + eX/2, y/2 + eY/2)
  draw.ellipse(bbox, fill = 'black', outline ='black')
  mask_im.save('circle.png')

I want to know how can I fix this problem.

Advertisement

Answer

To draw a circle of radius 50 with center at (150,150):

  mask_im = Image.new("RGB", (300,300), (53, 108, 181))
  draw = ImageDraw.Draw(mask_im)

  X, Y = 150, 150
  r = 50
  
  draw.ellipse([(X-r, Y-r), (X+r, Y+r)], fill = 'black', outline ='black')

To the ellipse you have to provide

Two points to define the bounding box. Sequence of [(x0, y0), (x1, y1)] or [x0, y0, x1, y1], where x1 >= x0 and y1 >= y0

So for the the bottom left corner of the bounding box we have to move r units backwards from center on X axis and r units downwards from center on y axis. Similarly for the top right corner of the bounding box we have to move r units forwards from center on X axis and r units upwards from center on y axis.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement