I have a .fit file. I have read the file, displayed the image with scale. When I want to write this image in .png file, the .png file is displaying the image without scale. I am attaching the code that I have tried.
import pyfits
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
hdulist = pyfits.open('HMI20170425_134641_6173.fits')
image_data = hdulist[0].data
hdulist.close()
fig=plt.imshow(image_data, cmap='gray')
plt.colorbar()
fig.write_png('image.png')
It is showing output image with scale. However, the 'image.png' file showing image without scale.
Please help me in this regard.
I guess what you call the scale is actually the colorbar ? Which indeed is missing when you use fig.write_png because here you are saving only the image part of the plot. You should use plt.savefig instead:
# use astropy instead of pyfits which is no more maintained
import astropy.io.fits as pyfits
import matplotlib.pyplot as plt
%matplotlib inline
image_data = pyfits.getdata('HMI20170425_134641_6173.fits')
plt.imshow(image_data, cmap='gray')
plt.colorbar()
plt.savefig('image.png')
Related
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
fig,ax=plt.subplots(2,2,figsize=(15,10))
x=np.linspace(-3,3)
ax[0,0].plot(x,foo-function)
now I need a way to save each of the 4 plots into one file like this:
plt1=topleft_plot.saveNOTfigBUTplot('quadfunction.pdf')
how?
Using the answer here: https://stackoverflow.com/a/4328608/16299117
We can do the following to save a SINGLE subplot from the overall figure:
import matplotlib.pyplot as plt
import numpy as np
fig,ax=plt.subplots(2,2,figsize=(15,10))
x=np.linspace(-3,3)
ax[0,0].plot(x,x**2) # This is just to make an actual plot.
# I am not using jupyter notebook, so I use this to show it instead of %inline
plt.show()
# Getting only the axes specified by ax[0,0]
extent = ax[0,0].get_window_extent().transformed(fig.dpi_scale_trans.inverted())
# Saving it to a pdf file.
fig.savefig('ax2_figure.pdf', bbox_inches=extent.expanded(1.1, 1.2))
EDIT: I believe I may have misunderstood what you want. If you want to save EACH plot individually, say as 4 different pages in a pdf, you can do the following adapted from this answer: https://stackoverflow.com/a/29435953/16299117
This will save each subplot from the figure as a different page in a single pdf.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
fig,ax=plt.subplots(2,2,figsize=(15,10))
x=np.linspace(-3,3)
ax[0,0].plot(x,x**2) # This is just to make an actual plot.
with PdfPages('foo.pdf') as pdf:
for x in range(ax.shape[0]):
for y in range(ax.shape[1]):
extent = ax[x, y].get_window_extent().transformed(fig.dpi_scale_trans.inverted())
pdf.savefig(bbox_inches=extent.expanded(1.1, 1.2))
I am new here and I need some help.
I have a gray image, and I need to colour it using Python.
This is the kind of images I have:
And I need to transform it to be like the images that can be plot by using matplotlib ColorMap "CMRmap" like this one and save it:
Thank you in advance for helping me.
Sounds like you've figured out the colormap part, but not the saving. Building on Shawn's answer, if you want to save the figure, make a call to plt.savefig() instead of plt.show(). Then pass the path you want to save it to as an argument.
import cv2
import matplotlib.pyplot as plt
img = cv2.imread(r"path\to\img", 0)
plt.imshow(img, cmap='CMRmap')
plt.savefig("\path\to\output\file")
Hope this helps!
Expanding on #Miki's comment, you simply need to use a colormap. The colored image shows the CMRmap colormap.
import cv2
import matplotlib.pyplot as plt
img = cv2.imread(r"path\to\img", 0)
plt.imshow(img, cmap='CMRmap')
plt.plot()
plt.savefig('foo.png')
Output:
Matplotlib lists all the colormaps here
Edit: updated answer with OP's clarification.
I need to load an image file with matplotlib and see the coordinates of points within it, as if it were a simple x,y scatter plot.
I can assume that the x axis extension is [0, 1], and the y axis follows the same scaling. I can load the above image file with
from PIL import Image
im = Image.open("del.png")
im.show()
but this uses ImageMagick (I'm on a Linux system) to display the image, and no coordinates are shown in the bottom left part of the plot window as would for a simple data plot:
Use pyplot for that:
from matplotlib import pyplot as plt
plt.imshow(plt.imread('del.png'))
How can I view images stored with a .npy extension and save my own files in that format?
.npy is the file extension for numpy arrays - you can read them using numpy.load:
import numpy as np
img_array = np.load('filename.npy')
One of the easiest ways to view them is using matplotlib's imshow function:
from matplotlib import pyplot as plt
plt.imshow(img_array, cmap='gray')
plt.show()
You could also use PIL or pillow:
from PIL import Image
im = Image.fromarray(img_array)
# this might fail if `img_array` contains a data type that is not supported by PIL,
# in which case you could try casting it to a different dtype e.g.:
# im = Image.fromarray(img_array.astype(np.uint8))
im.show()
These functions aren't part of the Python standard library, so you may need to install matplotlib and/or PIL/pillow if you haven't already. I'm also assuming that the files are either 2D [rows, cols] (black and white) or 3D [rows, cols, rgb(a)] (color) arrays of pixel values.
Thanks Ali_m. In my case I inspect the npy file to check how many images was in the file with:
from PIL import Image
import numpy as np
data = np.load('imgs.npy')
data.shape
then I plotted the images in a loop:
from matplotlib import pyplot as plt
for i in range(len(data)):
plt.imshow(data[i], cmap='gray')
plt.show()
When I try the following code, I get a picture of lena displayed on x and y axes, just fine:
import numpy
import pylab as lib
from PIL import Image
from skimage.viewer import ImageViewer
from scipy import misc
pl.imshow(misc.lena(),cmap=pl.gray())
pl.show()
But when I read in and then try to display my own image from file, like this:
image1 = color.rgb2gray(io.imread("PATH_TO_IMAGE\\akaria1.jpg"))
pl.imshow(Image.fromarray(image1),cmap=pl.gray())
pl.show()
then I do get the axes and everything, but in place of the image, just blackness taking up the space on the axes. Screenshot below:
However, I do know that I've read my image in fine, because when I do this:
image1 = color.rgb2gray(io.imread("C:\\work_asaaki\\caltech\\cars_brad\\akaria1.jpg"))
iv = ImageViewer(image1)
iv.show()
then I do get the image displayed in ImageViewer.
But what's the problem in the previous block of code? How can I get pylab to display my own image files just like it does lena? I'm running Windows 7.
You have to use:
pl.imshow(image1, cmap=pl.gray())