15 Mar Display an Image with OpenCV
Here’s a simple Python script to load and display an image using OpenCV. The imread() function is used to display an image with OpenCV:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Read and display an image with OpenCV import cv2 # Load an image image = cv2.imread(r'C:\Users\hp\Downloads\amit.jpeg') # Display the image cv2.imshow("My YouTube Channel's cake", image) # Wait for a key press and close the window cv2.waitKey(0) cv2.destroyAllWindows() |
The following is the output:
Explanation:
This above Python script uses the OpenCV library to load and display an image. The script begins by importing the OpenCV library.:
- It then reads an image from the specified file path and loads it into a variable named image.
- The cv2.imread() function is used for this purpose, reading the image in its original color format.
- Next, the script displays the image in a window with the title “My YouTube Channel’s cake” using the cv2.imshow() function.
- To ensure the image window remains open, the script includes a cv2.waitKey(0) function, which waits indefinitely for a key press.
- Once a key is pressed, the script closes all OpenCV windows using the cv2.destroyAllWindows() function. This process allows users to view the loaded image and close the display window at their convenience.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
- Generative AI Tutorial
- Machine Learning Tutorial
- Deep Learning Tutorial
- Ollama Tutorial
- Retrieval Augmented Generation (RAG) Tutorial
- Copilot Tutorial
- Gemini Tutorial
- ChatGPT Tutorial
No Comments