How to Count Color Objects Using PySimpleGUI

Posted by


PySimpleGUI is a Python library that allows you to easily create graphical user interfaces (GUI) for your Python programs. In this tutorial, we will be using PySimpleGUI to count the number of color objects in an image.

To get started, first you need to install PySimpleGUI by running the following command in your terminal:

pip install PySimpleGUI

Next, you will need to have an image that you want to count the color objects in. You can download an image from the internet, or use one of your own images.

Now, let’s create a Python script that counts the number of color objects in the image using PySimpleGUI. Here’s the code for the script:

import PySimpleGUI as sg
from PIL import Image
import numpy as np
from collections import Counter

def count_color_objects(image_path):
    # Load the image using Pillow
    image = Image.open(image_path)

    # Convert the image to a numpy array
    image_array = np.array(image)

    # Flatten the image array
    flat_image_array = image_array.reshape(-1, 3)

    # Count the occurences of each unique color in the image
    color_counts = Counter(tuple(pixel) for pixel in flat_image_array)

    # Return the number of unique colors in the image
    return len(color_counts)

# Create a PySimpleGUI window
layout = [
    [sg.Text('Enter the path to the image:')],
    [sg.Input(), sg.FileBrowse()],
    [sg.Button('Count')],
    [sg.Text('Number of color objects:'), sg.Text('', key='output')]
]

window = sg.Window('Color Object Counter', layout)

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

    if event == sg.WIN_CLOSED:
        break

    if event == 'Count':
        image_path = values[0]
        num_color_objects = count_color_objects(image_path)
        window['output'].update(num_color_objects)

window.close()

In this script, we first define a function count_color_objects that takes the path to an image as input, loads the image using Pillow, converts it to a numpy array, flattens the array, counts the occurrences of each unique color in the image, and returns the number of unique colors.

We then create a PySimpleGUI window with an input field for the image path, a button to trigger the counting, and a text field to display the number of color objects.

Finally, we enter an event loop where we wait for the user to input the image path and click the count button. When the count button is clicked, we call the count_color_objects function with the image path as input and update the text field with the number of color objects.

To run the script, save it to a Python file (e.g. color_object_counter.py) and run it in your terminal. You can then enter the path to the image you want to count the color objects in, click the count button, and see the number of color objects displayed in the PySimpleGUI window.

That’s it! You now have a Python script that counts the number of color objects in an image using PySimpleGUI. Feel free to customize the script further to suit your needs or add more features to enhance the functionality.

0 0 votes
Article Rating

Leave a Reply

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@yosuachristmas7240
11 hours ago

Boleh minta programnya?

@PySimpleGUI
11 hours ago

Oh Wow…. just WOW

Do you have a GitHub for this?!

Seriously impressive looking stuff. I love the AI uses of PySimpleGUI and it looks like you've made a cool one.

2
0
Would love your thoughts, please comment.x
()
x