VS CODE not showing Matplotlib images - python

I have a simple script that reads and shows images in Matplotlib.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
baseimg = '/home/ahmad/Downloads/mnist/trainingSample/img_0.jpg'
img = mpimg.imread(baseimg)
plt.imshow(img)
plt.show()
It is working on Jupyter Notebook but when I run it in VS Code, it runs but does not show any image. Is there some problem in my code or how to fix it. Thanks

Related

VsCode matplotlib doesnt show figure window

import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread("Desktop\sudoku.jpg", 0)
img = np.float32(img)
plt.figure()
plt.plot()
plt.imshow(img), plt.axis("off")
cv2.waitKey(0)
code doesn't give error but figure window is not opening
I think you are mixing cv2 and matplotlib. If you just want to display the image you can do it like so:
Using cv2:
import cv2
I = cv2.imread('path/to/img')
cv2.imshow('window_name', I)
cv2.waitKey(0)
or you can use matplotlib by simply adding plt.show() at the end as #JeruLuke pointed out. Just remember that OpenCV reads the image as BGR and this would make the colors show up weird when you display the image. You can use cv2.imread('path/to/image.jpg', COLOR_BGR2RGB) if you are set on using matplotlib.

How do you save an image displayed in Spyder Plots panel programmatically?

I have recently attempted to save images in Spyder using the pyplot.savefig() command as follows (imported from matplotlib), but it doesn't work:
E.g. code:
filename = 'myImage.png'
#images contain the image shown in the Spyder Plots pane
pyplot.imshow(images)
pyplot.savefig(filename)
I am aware that the Plots pane has a Save button, but I want to save the image programmatically (using Python).
Please advise.
Thanks!
Use plt.imsave() to save the image. Here is the code:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('test.png')
plt.imshow(img)
plt.imsave("myImage.png", img)
plt.show()

How do you log stretch a FITS image and change its contrast?

I'm trying to use astropy 2.0.11 with python 2.7.15 to edit a fits image by applying a log stretch to it and change the contrast, and I have't been able to figure it out.
I've been trying to follow the tutorials on the astropy website for opening and manipulating fits files, but I'm wondering if the tutorials will only work for the latest version of astropy and on python 3?
Sorry about the organization of my code. This is prototype code and I'm just trying to test a few things and get this to work.
import time
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import astropy.visualization
from astropy.io import fits
from astropy.utils.data import download_file
from astropy.visualization import astropy_mpl_style
plt.style.use(astropy_mpl_style)
from astropy.utils.data import get_pkg_data_filename
def main():
#My own fits file
#fitsImage = get_pkg_data_filename("C:\\20180807T000456.fits")
fitsImage = download_file('http://data.astropy.org/tutorials/FITS-images/HorseHead.fits', cache=True )
hdu_list = fits.open(fitsImage)
hdu_list.info()
#norm = ImageNormalize(stretch=LogStretch())
image_data = fits.getdata(fitsImage)
print(type(image_data))
print(image_data.shape)
hdu_list.close()
plt.figure()
plt.imshow(image_data, cmap='gray', norm=LogNorm())
plt.colorbar()
# I chose the tick marks based on the histogram above
cbar = plt.colorbar(ticks=[5.e3,1.e4,2.e4])
cbar.ax.set_yticklabels(['5,000','10,000','20,000'])
time.sleep(10)
I am also unable to get the image to display with the plt.imshow()
Any insight would be helpful
You're so close! I ran your code in Python 2.7 and all you need to do is add
plt.show()
before time.sleep(10) (any reason you're including this?) and you get
Also, I don't think you need to include the colorbar and yticklabels, plt.imshow automatically adds the colorbar with the lognorm scale (I commented that section out when I got the image).

Strange image produced by matplotlib

I have a png image as follows:
I wrote the following script to read into matplotlib.
import numpy as np, matplotlib.pyplot as plt
from PIL import Image
fi = "map.png"
data = np.array(Image.open(fi))
print data.shape
plt.imshow(data)
plt.show()
But the image looks different.
How to make it look similiar to the first one?
I mean, the colors.
Matplotlib handles images much more transparently if you use the mpimg package:
import numpy as np, matplotlib.pyplot as plt
import matplotlib.image as mpimg
fi = "map.png"
data = mpimg.imread(fi)
print(data.shape)
plt.imshow(data)
plt.show()
The image you have provided is not a Grayscale image
import matplotlib.pyplot as plot
import matplotlib.image as image_rgb
image = image_rgb.imread("image.png")
plot.imshow(image)
plot.show()
I have not used the PIL library and it produces apt results.

Image does not show with matplotlib.pyplot with ipython or python

If I do the following commands in iPython or just Python,
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img=mpimg.imread('stinkbug.png')
imgplot = plt.imshow(img)
then nothing happens (no image appear anywhere).
But if I do the following commands:
import scipy.misc as misc
img=misc.imread('stinkbug.png')
misc.imshow(img)
then image appears inside separate window of ImageMagick.
Also, I can run ipython with qtconsole and will see image with first code.
What are the difference between two different ways of diplaying images? Can they be unified, i.e. work in similar way in both consoles? Is it possible to make first code work in normal ipython/python?
Add plt.show(imgplot) at the end of your code.
One line is missing to show the plot window:
plt.show()

Categories