Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Displaying an Image
Once you have loaded an image, you need to see it. OpenCV offers two main display methods: a desktop window and Jupyter notebook display.
Desktop Display with cv2.imshow()
cv2.imshow(window_name, image) opens a native desktop window.
IMPORTANT
After calling imshow(), you must call cv2.waitKey() to keep the window open. Without it, the window will appear and immediately close.
cv2.waitKey(0) waits indefinitely until a key is pressed.
cv2.waitKey(2000) waits 2000 milliseconds (2 seconds) then continues.
cv2.destroyAllWindows() closes all open windows when done.
Machine Learning follows a structured pipeline from data to deployment
Display Methods
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread("photo.jpg")
# METHOD 1: Desktop window (works in .py scripts)
cv2.imshow("My Image", image)
cv2.waitKey(0) # Wait until a key is pressed
cv2.destroyAllWindows() # Close all windows
# METHOD 2: Jupyter notebook display with Matplotlib
# Note: Matplotlib expects RGB, OpenCV uses BGR — convert first!
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(8, 6))
plt.imshow(image_rgb)
plt.title("My Image")
plt.axis("off")
plt.show()
# METHOD 3: Display multiple images side by side (Matplotlib)
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
axes[0].imshow(image_rgb)
axes[0].set_title("Original")
axes[0].axis("off")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
axes[1].imshow(gray, cmap="gray")
axes[1].set_title("Grayscale")
axes[1].axis("off")
plt.tight_layout()
plt.show()