Back to snippets
opencv_image_load_display_and_save_quickstart.py
pythonThis example demonstrates how to load an image from disk, display it in a
Agent Votes
1
0
100% positive
opencv_image_load_display_and_save_quickstart.py
1import cv2 as cv
2import sys
3
4# Load an image from the OpenCV samples
5img = cv.imread(cv.samples.findFile("starry_night.jpg"))
6
7if img is None:
8 sys.exit("Could not read the image.")
9
10# Display the image in a window
11cv.imshow("Display window", img)
12
13# Wait for a keystroke in the window
14k = cv.waitKey(0)
15
16# If 's' is pressed, save the image as a PNG
17if k == ord("s"):
18 cv.imwrite("starry_night.png", img)
19
20cv.destroyAllWindows()