Load and display image in Python and OpenCV OpenCV will be used to load a local image from the system while PIL will be used to display the image from the terminal. We have specified the color coding of the image that is RGB. This will open a new terminal window that will display the local image.
If you are using matplotlib and want to show the image in your interactive notebook, try the following:
%pylab inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('your_image.png')
imgplot = plt.imshow(img)
plt.show()
If you use matplotlib
, you need to show the image using plt.show()
unless you are not in interactive mode.
E.g.:
plt.figure()
plt.imshow(sample_image)
plt.show() # display it
In a much simpler way, you can do the same using
from PIL import Image
image = Image.open('image.jpg')
image.show()
Using opencv-python is faster for more operation on image:
import cv2
import matplotlib.pyplot as plt
im = cv2.imread('image.jpg')
im_resized = cv2.resize(im, (224, 224), interpolation=cv2.INTER_LINEAR)
plt.imshow(cv2.cvtColor(im_resized, cv2.COLOR_BGR2RGB))
plt.show()
Your first suggestion works for me
from IPython.display import display, Image
display(Image(filename='path/to/image.jpg'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With