Click here to Skip to main content
15,887,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to draw a circle on image using numpy only, without using anyother library.

What I have tried:

from PIL import Image 
image=Image.open("rgb.png")
arr=np.array(image)
arr[50,50]=[255]
arr[50,30]=[255]
'''for i in range(50):
    arr[i,50-i-1]=[255]'''
arr[30,50]=[255]
arr[50,70]=[255]
arr[70,50]=[255]


img=Image.fromarray(arr,"RGB")
img.save('arr.png')
img.show()
Posted
Updated 25-Aug-22 21:28pm

Drawing a circle is easy: all you need is a loop and the right equations:
px = cx + r * cos(a)
py = cy + r * sin(a)
Where the center of the circle with radius r is (cx, cy), and a is the angle which changes as you go round the circle drawing the points / lines (px, py).
The smaller the change in angle each time round your loop, the better the resolution of the circle.
So if you want a circle that is actually square, the angle has five values: 0o, 90o, 180o, 270o, and 360o (or their equivalent in radians) though the first and last will generate the same point. More pints, better looking circle - as the number of points you need obviously increases as the radius of the circle does because the length of its circumference is proportional to that radius.
 
Share this answer
 
Comments
Member 15667238 26-Aug-22 1:26am    
okay, but how to draw the circle in numpy array.
CPallini 26-Aug-22 3:29am    
5.
Quote:
okay, but how to draw the circle in numpy array.
Try
Python
from PIL import Image
import numpy as np
image=Image.open("rgb.png")
arr=np.array(image)

r = image.width/2 # the radius of the circle (assuming a square image)

for angle in np.arange(0, 2 * np.pi, 1/2/r):
  x = int(r-1 + r * np.cos(angle))
  y = int(r-1 + r * np.sin(angle))
  arr[x,y] = [255]

img=Image.fromarray(arr,'RGB')
img.save('arr.png')
img.show()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900