This GUI-based approach turns a normal, simple exercise about image resizing and binary image thresholding into a delightful interactive experience akin to playing with digital building blocks. Gone are the days of tediously typing commands into a terminal or wrestling with complicated software interfaces. With just a few clicks and drags, users can effortlessly manipulate images.
There are 2 requirements for this prorgam: Resize photo and display binary image. For each task, there are 2 files (as shown on the figure above): A GUI file that create the GUI windows, accept user inputs and parse them to the back end files (ImgResize and MakePhotoBinary). The structure can be shown in the diagram below:
+-----------------------+ +-----------------------+ +-----------------------+
| GUI File | | Backend File | | Application |
+-----------------------+ +-----------------------+ +-----------------------+
| - title: customtkinter| | | | - app: CTk |
| - height_var: StringVar| | | +-----------------------+
| - width_var: StringVar| | |
| | | |
| + handle_resize() | | + image_resize() |
| + handle_path_input() | | |
+-----------------------+ +-----------------------+
| |
| |
| |
| |
| |
+--------------------------------+
|
|
|
|
v
+-----------------------+
| Libraries |
+-----------------------+
| - OpenCV |
| - Matplotlib |
+-----------------------+
After downloading, your IDE will show 4 files: GUI_ImgResize, GUI_MakePhotoBinary, ImgResize, MakePhotoBinary. The program wll only run from the appopriate GUI file.
First, run the GUI_ImgResize
file. A window will pop up, like this:
Enter the height and the width (in pixel) of your photo that the resized image would be in. For example, if you want to resize the image to 1280x720, enter like this:
Then select the image you want to resize:
- IMPORTANT: File name must not contain Unicode character (other than English character).
Press "Resize Image", and the resized image will be displayed:
First, run the GUI_MakePhotoBinary
file. A window will pop up, like this:
Enter the binary threshold (must be between 0 and 255). For the best result, It's recommended set the binary value to 128:
If you enter an out-of-bound binary threshold, a error message will pop up:
Then select the image you want to display binary image:
Press "Display Binary Image", and a Mathplotlib plot will show both the original (in RGB color, if selected) and the binary image:
Yes. You will need the following:
- customtkinter (install by
pip install customtkinter==0.3
) - OpenCV (install by
pip install opencv-python
) - Mathplotlib (install by
pip install matplotlib
) (tkinter is buitt-in in Python, but you still need to import it by:)
import tkinter
This Assignment made extensive use of the CustomTkinter library.
For debugging, I want to thanks the following StackOverflow thread for the following fixes: