In this Python TKinter article we are going to learn How to Integrate OpenCV with Python TKinter, so OpenCV is popular and open source computer vision library that provides different image processing and computer vision algorithms. Tkinter is builtin Python GUI library used to develop desktop applications. in this article we want to talk that integrating OpenCV with Tkinter to develop image processing applications.
How to Integrate OpenCV with Python TKinter
TKinter is already built in with Python, we need to install OpenCV for Python.
1 |
pip install opencv-python |
Now let’s create our basic GUI window with Python TKinter, using this code we can create simple GUI Window in Python TKinter.
1 2 3 4 |
import tkinter as tk root = tk.Tk() root.mainloop() |
Run the code and you will see this result
Now let’s learn that how we can integrate Python TKinter with OpenCV, to integrate OpenCV with Tkinter, you can use cv2 module to read and process images and PIL (Python Imaging Library) module to convert the images into a Tkinter compatible format.
This is the code that how we can show image using TKinter and OpenCV, make sure that you already have installed PIL library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import cv2 from PIL import Image, ImageTk import tkinter as tk root = tk.Tk() # Load image using OpenCV image = cv2.imread('python.png') # Convert image to RGB image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert image to PIL format image = Image.fromarray(image) # Create Tkinter compatible image photo = ImageTk.PhotoImage(image) # Display image label = tk.Label(image=photo) label.pack() root.mainloop() |
Using this code we can read our image with cv2.imread() method.
1 |
image = cv2.imread('python.png') |
In here we have convereted the image to RGB, because we are reading the image using OpenCV, and OpenCV uses BGR color system and we need to convert that to RGB.
1 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
In here we have converted the image to PIL format using Image
1 |
image = Image.fromarray(image) |
In here the image is converted to TKinter Compatible format
1 |
photo = ImageTk.PhotoImage(image) |
Also a label is created to display the image
1 2 |
label = tk.Label(image=photo) label.pack() |
Run the code this is the result
Also you can do image processing, for example in this code we are converting the image to grayscale
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import cv2 from PIL import Image, ImageTk import tkinter as tk root = tk.Tk() root.title("GeeksCoders - TKinter & OpenCV") # Load image using OpenCV image = cv2.imread('python.png') # Convert image to grayscale gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Convert grayscale image to PIL format image = Image.fromarray(gray_image) # Create Tkinter compatible image photo = ImageTk.PhotoImage(image) # Display image label = tk.Label(image=photo) label.pack() root.mainloop() |
above code reads an image using cv2.imread, applies cv2.cvtColor function with the cv2.COLOR_BGR2GRAY argument to convert the image to grayscale, and then converts the grayscale image to PIL format. and at the end Tkinter compatible image is created using ImageTk.PhotoImage, and Label is created to display the image.
If you run the code this will be the result
Learn More on Python
- How to Integrate PyQt6 with OpenCV
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6