PySimpleGUI: How to Draw Shapes on an Image With a GUI
Let's add some flair to that image.
Join the DZone community and get the full member experience.Join For Free
Drawing shapes on images is neat. But wouldn’t it be nice if you could draw the shapes interactively? That is the point of this tutorial. You will create a user interface using PySimpleGUI to allow you to draw shapes on images!
The purpose of this user interface is to show you how you can make a GUI that wraps some of the shapes that Pillow supports. The GUI won’t support all of them though. In fact, this GUI only supports the following:
The reason for this is that these two shapes take the same arguments. You can take on the challenge yourself to add the other shapes to the GUI!
When your GUI is finished, it will look like this:
You will need PySimpleGUI and Pillow to be able to follow along with this tutorial. Here’s how to install them with pip:
python3 -m pip install PySimpleGUI pillow
Now you’re ready to create your GUI!
Creating the GUI
Now open up your Python editor and create a new file named
drawing_gui.py. Then add this code to your new file:
That’s a bunch of code! To make things easier, you will go over this code in smaller chunks.
The first chunk is the code at the top of the file:
These lines of code define the imports of the packages and modules that you need. It also sets up two variables:
file_types– Which you will use for browsing and saving your images
tmp_file– A temporary file that is created to save your intermediate image file
Now you can take a look at your first function:
This function is used to convert strings to integers. If you enter an alphabetical character or a special character, it will cause this code to throw an error. Feel free to catch those kinds of things here or implement a filter to prevent users from entering anything other than integers.
If the user empties the Element of its contents, you force it to return a zero. This allows the user interface to continue to function. Another improvement that you could add here is to take the image bounds into account. You could make it so that the user cannot enter negative numbers or numbers that are larger than the image.
The next function is a meaty one:
This code is used for creating the two shapes you want to draw on your image. It gets all the various settings that you need to create an ellipse or a rectangle. The settings that you can change are:
- The image filename
- The shape combobox
- The beginning coordinates (
- The ending coordinates (
- The width of the outline
- The fill color to be used (uses color names)
- The outline color (uses color names)
If the user has opened up an image, then your code will automatically draw a shape using the default settings. When you edit any of those settings, this function will get called and the image will update.
Note: The drawing is applied to a thumbnail version of the image rather than a copy of the image. This is done to keep the image visible in your GUI. Many photos have a higher resolution than can be shown on a typical monitor.
The next function to look at is called
This function returns a Python list that contains three Elements in it. One label (
sg.Text) and two text boxes (
sg.Input). Because these elements are in a single list, they will be added as a horizontal row to your user interface.
Now you’re ready to go on to the
This function asks the user where to save your file. It will also prevent the user from trying to overwrite the original image.
The last function is your
This is one of the most complex user interfaces you have written. It adds lots of Elements in rows. You can also see that you are using a special syntax to extract items from a list:
When you call
create_coords_elements(), it returns a list. But you want both the beginning and ending coordinates on the same line. So you extract the elements from the list.
This little code example illustrates what is happening:
If you don’t use the asterisk, you will end up with a nested list instead of a list that has three elements in it.
Here are the last few lines of code from the
The first line defines the events that you use to know when to update the drawing. The last conditional will save the image where the user chooses. Give your new GUI a try. Then you can start planning how you will improve it!
Now you have a handy tool for testing out a couple of Pillow’s drawing functions. You can make this GUI even better by adding some of Pillow’s other shapes to the mix. You could do that by adding a tabbed interface with the different options under different tabs, for example.
Give it a try and see what you can come up with!
Published at DZone with permission of Mike Driscoll, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.