- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Read an image with OpenCV and display it with Tkinter
OpenCV is an Open Source Computer Vision library in Python which is widely used for Research purposes in Artificial Intelligence and Machine Learning. Computer Vision Library such as OpenCV deals with image processing. We can use OpenCV to read an image and further use it for development.
Let us suppose that we want to create an application that reads an image and displays it in the window using OpenCV.
Install OpenCV by using the following command −
pip install opencv-python
Next, follow the steps given below −
Install OpenCV in the environment and import the library using import cv2.
Import NumPy and PIL (Pillow Package) for image calculation.
Load the Image using imread(image_location) function.
Split the RGB Color of the image using the split(image) function.
Merge the Image colors using merge(rgb) function.
Convert the multidimensional matrix into an image.
Convert the given image using PhotoImage(image= file) function.
Initialize a label and display the Image.
Example
#Import the tkinter library from tkinter import * import numpy as np import cv2 from PIL import Image, ImageTk #Create an instance of tkinter frame win = Tk() win.geometry("700x550") #Load the image img = cv2.imread('tutorialspoint.png') #Rearrange colors blue,green,red = cv2.split(img) img = cv2.merge((red,green,blue)) im = Image.fromarray(img) imgtk = ImageTk.PhotoImage(image=im) #Create a Label to display the image Label(win, image= imgtk).pack() win.mainloop()
Output
Running the above code will load and display an image in the window.
Make sure the image 'tutorialspoint.png' is located in the same folder as the program.
- Related Articles
- Using OpenCV with Tkinter
- How to read an image in Python OpenCV?
- How to rotate an image with OpenCV using Java?
- How to display an image/screenshot in a Python Tkinter window without saving it?
- How can I display an image using Pillow in Tkinter?
- How to Move an Image in Tkinter canvas with Arrow Keys?
- How to read an input image and print it into an array in matplotlib?
- Opencv Python – How to display the coordinates of points clicked on an image?
- Upsampling an image using OpenCV
- Downsampling an image using OpenCV
- How to read an image file in external storage with runtime permission in android?
- How to display a Listbox with columns using Tkinter?
- How to display an image with rounded corners on iOS App using Swift?
- Draw an ellipse on an image using OpenCV
- Display a blue shadow on image hover with CSS
