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

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()

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.

VS CODE not showing Matplotlib images

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

Import image in python 3 with pillow

For a few hours now, I've been trying to figure out how to view an image I imported with pillow. So far, I got rid of all errors, but it just shows the exact filename of the photo, the image isn't showing up, please help me fix this.
This is what I have so far:
from PIL import Image
img = Image.open("image.jpg")
print(img)
From the docs the function to show an image is
Image.show(title=None, command=None)
So to display an image you can do
from PIL import Image
img = Image.open("image.jpg")
img.show()
Sir Please try this code
from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline
a=Image.open("F:\\FYP DATASET\\images\\train\\10_left.jpeg")
a
try by removing print statement and adding matplotlib library.
Purpose of %Matplotlib inline in Python:
By just writing variable without print statement will display the image in notebook and by img.show() It will display image in Photo Viewer (Windows photo viwer) etc depends on what you are using.
from PIL import Image
img = Image.open("man.png")
img.show()

matplotlib color images are not displayed when used in the command prompt (pdb mode)

When running python code on the command-line, python test.py,
matplotlib doesn't show color images but rather display it as gray images.
Any workaround for displaying images when run on the command line?
demo code:
import matplotlib.pyplot as plt
import cv2
img = cv2.imread('color-image.jpg')
plt.imshow(img)
plt.show() # shows gray image
You do need to tell OpenCV that you're reading a color image, for example with this:
image = imread(argv[1], CV_LOAD_IMAGE_COLOR);
If you don't use it like that it will load the image grayscale by default.
Please change the subject as this is not Matplotlib error but OpenCV. There are plenty of examples on the OpenCV documentation:
https://docs.opencv.org/2.4/doc/tutorials/introduction/display_image/display_image.html

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