Python for Everyone: Image Processing with PySimpleGUI, Part 5

Posted by


Welcome to the 113th installment of だれでもPython! In this tutorial, we will be exploring how to perform image processing using PySimpleGUI, a powerful Python library for creating graphical user interfaces. By the end of this tutorial, you will have a solid understanding of how to use PySimpleGUI to manipulate images in various ways.

To begin, make sure you have PySimpleGUI installed on your system. You can install it using pip by running the following command:

pip install PySimpleGUI

Next, you will need to have some sample images to work with. You can download images from the internet or use any images you have on your computer. For this tutorial, I will be using a sample image called "image.jpg".

Now, let’s dive into the code. Start by importing the necessary modules:

import PySimpleGUI as sg
import cv2
import numpy as np

Next, let’s define the layout of our GUI using PySimpleGUI:

layout = [
    [sg.Text('Enter the name of the image file:'), sg.InputText(key='-FILENAME-'), sg.FileBrowse()],
    [sg.Button('Load Image'), sg.Button('Blur Image'), sg.Button('Save Image')],
    [sg.Image(filename='', key='-IMAGE-')]
]

This layout consists of an input field for the file name, a button to load the image, a button to blur the image, a button to save the image, and an image element to display the processed image.

Now, let’s create the window using PySimpleGUI and set its layout:

window = sg.Window('Image Processing', layout)

Next, we will define the image processing functions. Let’s start with loading the image:

def load_image(filename):
    image = cv2.imread(filename)
    return image

Next, let’s define the function to blur the image:

def blur_image(image):
    blurred_image = cv2.GaussianBlur(image, (15, 15), 0)
    return blurred_image

Now, let’s define the main event loop:

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    elif event == 'Load Image':
        filename = values['-FILENAME-']
        image = load_image(filename)
        window['-IMAGE-'].update(data=cv2.imencode('.png', image)[1].tobytes())

    elif event == 'Blur Image':
        blurred_image = blur_image(image)
        window['-IMAGE-'].update(data=cv2.imencode('.png', blurred_image)[1].tobytes())

    elif event == 'Save Image':
        filename = sg.popup_get_file('Save Image', save_as=True, file_types=(("PNG Files", "*.png"),))
        cv2.imwrite(filename, blurred_image)

In this event loop, we check for different button presses and call the corresponding image processing functions. We update the image element in the window to display the processed image.

Finally, let’s add the code to close the window and clean up resources:

window.close()

And that’s it! You have now created a Python program using PySimpleGUI to perform image processing. You can expand on this program by adding more image processing functions or integrating other libraries for more advanced image manipulation. Experiment with different image processing techniques and have fun exploring the possibilities with PySimpleGUI!

I hope you found this tutorial helpful and informative. Thank you for joining me for the 113th installment of だれでもPython. Stay tuned for more exciting tutorials in the future!