I have a very high resolution(3311, 4681, 3) image, which I want to show in my jupyter notebook using opencv but as other answers stated its not possible to use cv2.imshow in the jupyter notebook, so i used plt.imshow to do the same but the problem is I have to define the fig_size parameter if I want to display my image larger. How can I read the image in its original resolution in jupyter notebook or is it possible to open the image in another window?
This is what I have tried :
import cv2
from matplotlib import pyplot as plt
%matplotlib inline
img = cv2.imread(r"0261b27431-07_D_01.jpg")
plt.figure(figsize= (20,20))
plt.imshow(img)
plt.show()
So basically I want my image to show in its original resolution in jupyter notebook or in another window.
You can imshow the image in its original resolution by calculating the corresponding figure size, which depends on the dpi (dots per inch) value of matplotlib. The default value is 100 dpi and is stored in matplotlib.rcParams['figure.dpi'].
So imshowing the image like this
import cv2
from matplotlib import pyplot as plt
import matplotlib
%matplotlib inline
# Acquire default dots per inch value of matplotlib
dpi = matplotlib.rcParams['figure.dpi']
img = cv2.imread(r'0261b27431-07_D_01.jpg')
# Determine the figures size in inches to fit your image
height, width, depth = img.shape
figsize = width / float(dpi), height / float(dpi)
plt.figure(figsize=figsize)
plt.imshow(img)
plt.show()
prints it in its large resolution, but with the drawback, that the axis labels are tiny compared to the large image. You can workaround this by setting other rcParams to larger values, e.g.
# Do the same also for the 'y' axis
matplotlib.rcParams['xtick.labelsize'] = 50
matplotlib.rcParams['xtick.major.size'] = 15
matplotlib.rcParams['xtick.major.width'] = 5
...
Your second suggestion to open the image in another window would work like this, that you change the matplotlib backend using Ipython magic commands by replacing %matplotlib inline in the above example with, e.g.
%matplotlib qt # opens the image in an interactive window with original resolution
or
%matplotlib notebook # opens the image in an interactive window 'inline'
See here for more backend possibilites. Note that the calculation of the original figure size has to be done before also.
In order to show image in full original resolution,
You can also use PIL
from PIL import Image
img = Image.open(r'0261b27431-07_D_01.jpg')
img
Related
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()
I'm learning some very basic steganography using python.
So far, I've been opening the file using:
from matplotlib import pyplot as plt
import numpy as np
file_location = '...'.png
rgb_data = np.array(plt.imread(file_location))
...
manually or otherwise edit some RGB values
...
plt.axis('off')
plt.imshow(rgb_data)
plt.savefig('image.jpg', dpi=96)
Note: In the above code, I've left out the particular edits I've been doing to the image. This is because I don't think they are part of my issue. Even if I make no changes at all, and just load then resave the image, I still encounter the issues below.
I've encountered three issues trying to save images using matplotlib in this way.
1. Getting the dpi to match
I'm not certain how to get the dpi of the output image to automatically match the dpi of the read image.
2. Getting the resolution to match
The resolutions of the input and output image by default don't match. Even if I manually match the dpi of the two images, they still don't have matching resolutions.
3. Getting the file sizes to match
My end goal is to produce an image that has the same dpi, resolution, and file size as the original. That way, when I start playing around with the RGB values, it should superficially appear to be the exact same image.
My question is how to save the file so the dpi and resolution (and presumably by extension the size...) of the output image match the input?
Of course, the number of pixels in the image, along with the dpi, should fix the resolution. However, it appears as though the output image is saved with a white border surrounding it, which is throwing off the resolution too.
Solutions using any python library are appreciated. However, edits to the existing code are preferable. Since I use matplotlib a lot, it would be useful to know how to circumvent this problem in the future too. Your help would be really appreciated!
plt.save(...) will save a Matplotlib figure. If you want to use only Matplotlib, please refer this post.
from matplotlib import pyplot as plt
import numpy as np
file_location = '...'.png
rgb_data = np.array(plt.imread(file_location))
# modify your image
fig = plt.figure(frameon=False)
h, w, _ = rgb_data.shape
fig.set_size_inches(w, h)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
ax.imshow(rgb_data, aspect='auto')
path_to_save = "..."
fig.savefig(path_to_save)
The output image should match the input image.
But I think it will be better to save your image by using pillow.
import PIL.Image as Image
import numpy as np
file_location = "img.png"
path_to_save = "out.png"
img = Image.open(file_location)
rgb_data = np.array(img)
# modify your image
rgb_data = Image.fromarray(rgb_data)
rgb_data.save(path_to_save, format="PNG")
# you can also specify dpi, quality and etc.
# for example rgb_data.save(path_to_save, format="PNG", dpi=(300,300))
(Note: it is possible that due to compression settings of the input image size, the output size will be different, refer to this post)
I have changed the format of the images to png also..but of no use. Does cv2 / imshow decrease the resolution automatically?
import numpy as np
import cv2
from matplotlib import pyplot as plt
imgL = cv2.imread('image.png',0)
imgR = cv2.imread('2.png',0)
stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)
disparity = stereo.compute(imgR,imgL)
plt.imshow(disparity, 'gray')
plt.show()
My main aim is to generate the final image with the resolution as was the supplied images.
You're using imshow from matplotlib which might be the cause of different showing behaviour.
Instead try:
cv2.imshow("Res", disparity)
cv2.waitKey(0)
cv2.destroyAllWindows()
If that is still not good, please edit the question and include the resulting image and the input image.
I'm trying to show a figure, in PyCharm, that is in my working directory, but it either doesn't work or only works with this code:
img = mpimg.imread('Figure_1.png')
plt.imshow(img)
plt.show()
with this result:
I just want the picture as it is, not inside another figure. This is the original picture for reference:
Some quick workarounds: to remove the axes, do plt.axes('off'). To make the picture fit the frame, set the aspect ratio to 'auto' and create a figure with the same aspect ratio as your original image (i'd say it's roughly 4:1?). Use tight_layoutto make sure all of your image is visible. I don't know if that's the official way, but that's how i do it, and it kinda works ;-)
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('Figure_1.png')
f, a = plt.subplots(figsize=(16, 4))
plt.tight_layout()
plt.axis('off')
a.imshow(img)
plt.show()
I'm using Matplotlib in a Jupyter Notebook to display an image of a map. The code looks like this:
%matplotlib inline
imgpath = './map.png'
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from PIL import Image
img = Image.open(imgpath)
print(img.size)
width, height = img.size
# img.thumbnail((width * 2,height * 2), Image.ANTIALIAS) # resizes image in-place
imgplot = plt.imshow(img)
plt.savefig('test.png', dpi = 300)
The problem is, although the plt.savefig('test.png', dpi = 300) looks fine (because I changed the dpi to 300), the image displayed in the notebook is so low resolution I can't make anything out on it, and plt.imshow(img, dpi = 300) doesn't work:
So what I'm wondering is if there is a way to change the resolution of the image shown in the Jupyter Notebook?
Add this at the beginning of the notebook:
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 300
That's it !
If your screen has Retina display, add the following line after %matplotlib inline (or somewhere else before plotting) in your notebook
%config InlineBackend.figure_format = 'retina'
This will increase the display resolution of your plots within the Jupyter Notebook.
Using the solution proposed in this answer has the drawbacks to apply the DPI to all the other figures that will be created within the notebook.
This is fine in most cases, but if you need to set the DPI value ONLY for one figure, you can do it as follows:
plt.imshow(img)
plt.gcf().set_dpi(300)
plt.show()