atom
atom

Reputation: 153

How to add a circle to image (figure) in python 2.7?

I have this code in python 2.7 :

import numpy as np
from astropy.io import fits
import matplotlib.pyplot as plt

image_file='test.fits'
hdu_list = fits.open(image_file)
image_data = hdu_list[0].data

plt.imshow(image_data,cmap='gray')
plt.show()

So I have opened a FITS file and saved its data into an array.

Up to this there is no any problem.

Now I have two questions.

My first question is: Now I want to add a circle (or write some text) at a particular location in the image. My data is an array of 2048 x 2048 float values.

My second question is: Suppose I save this figure (before drawing/adding anything on it) as "test.png". After saving as png image, now I want to add a circle (or write some text) at a particular location in the png image. How will I do this ?

Any help will be appreciated. Thanks.

Upvotes: 0

Views: 3789

Answers (3)

S.A.
S.A.

Reputation: 2151

I had a similar question and used the advice by Jerome Pitogo de Leon who answered earlier. For my case, I had a bmp image, which I saved to a variable 'img':

import matplotlib.pyplot as plt img = mpimg.imread('img_name.bmp') plt.show()

Then I checked, what the axes looked like, and specified my circles according to the coordinates (which I determined via visual inspection)

# Define circles
c1 = plt.Circle((100, 600), 50, color=(0, 0, 1))
c2 = plt.Circle((300, 400), 50, color=(1, 0, 0))
c3 = plt.Circle((500, 200), 50, color=(0, 1, 0))

# Open new figure
fig = plt.figure()

# In figure, Image as background
plt.imshow(img)

# Add the circles to figure as subplots
fig.add_subplot(111).add_artist(c1)
fig.add_subplot(111).add_artist(c2)
fig.add_subplot(111).add_artist(c3)

plt.show()

Upvotes: 1

JPdL
JPdL

Reputation: 149

Try something like:

hmask,wmask= 0.3,0.3 #radius = 15 pix = 20 AU
m=Ellipse(xy=(0,0),height=hmask,width=wmask)
figure(1).add_subplot(111).add_artist(m)
m.set_color('k')

Upvotes: 1

Christoph
Christoph

Reputation: 2940

If you have the circle positions given in pixel coordinates, you can use the scatter function from matplotlib or the Circle class. (In this case the plotting question has nothing to do with astropy really and you should tag it matplotlib instead)

If you have the circle positions given in world coordinates (i.e. longitude and latitude on the sky), you can use aplpy or wcsaxes.

About your second question: you can use matplotlib or scikit-image to load the png into a numpy array and then use again matplotlib to draw circles (but note that the axes coordinates will be different ... try to avoid this if you can or show an example with some code what exactly you want to do).

Upvotes: 1

Related Questions