DelphiPythonPython GUIWindows

Learn To Build A Python GUI For Easily Processing Images With Pillow Library In A Delphi Windows App

blogbanner3 26

Are you looking for a simple way to process images programmatically, and build a nice GUI for them? You can do it easily by combining Python4Delphi and Pillow library, inside Delphi and C++Builder. Python4Delphi (P4D) is a free tool that allows you to execute Python scripts, create new Python modules and types in Delphi.

Pillow or PIL is the Python Imaging Library that adds image processing capabilities to your Python interpreter. This library provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities. The core image library is designed for fast access to data stored in a few basic pixel formats. It should provide a solid foundation for a general image processing tool.

This post will guide you on how to manipulate an image via Python’s Pillow and then display it in the Delphi Windows GUI app.

First, open and run our Python GUI using project Demo01 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute script button, and get the result in the upper Memo. You can find the Demo01 source on GitHub. The behind-the-scenes details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

0 rundemo1 6813476

With the Pillow library, you can perform geometric and color transformations. It also allows you to cut, copy part of the image and merge several images into one. Let’s take a look at some examples.

1. Open, Show, and Get Image Properties

First, let’s open the image using the function open(). We can get image properties such as format, size, type or mode:

Run the script above in Demo01 GUI, we will get the following results:

1 1 9948495

2. Create Thumbnails or Resize Image

thumbnail() function allows us to create an image thumbnail. The input parameters of this function are the size of the image that we want to get in pixels. Use the save() function to save the image in a specified directory:

Run the script above, we will get the following result:

1 2 9895309

The cat image was resized to 250x167 (the previous image size is 1080x720).

3. Geometric Transformations

Function transpose() allows us to perform different geometrical transformations with the image. For example, we can rotate the image by a given angle or flip it horizontally or vertically:

Run the script above, we will get the following result:

1 3 4749554

Cool, wasn’t it?

4. Change Images Colors

Now let’s look at how to change image color automatically. The following code will change all the white (also shades of whites) pixels to yellow, and in the end, we merge everything into a new image:

Let’s see this interesting result:

1 4 7739875

Congratulations, now you have learned how to manipulate an image via Python’s Pillow and then display it in the Delphi Windows GUI App! Now you can make various modifications to your images using Pillow library and Python4Delphi. The only limitation is your imagination.


Check out the Pillow image processing for Python and use it in your projects: https://pypi.org/project/Pillow/ and

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi


References & further readings

[1] Hakim, M. A. (2023).

Article01 – Pillow (PIL). pythongui.orgRepo_Python4Delphi-Python-Libraries GitHub. github.com/MuhammadAzizulHakim/ pythongui.orgRepo_Python4Delphi-Python-Libraries/tree/main/Article01%20-%20Pillow%20(PIL)

[2] Lundh, F. (1995-2011), Clark, J. A. (2010-2023), and contributors.

Pillow (PIL Fork) 10.0.1 documentation – Handbook. readthedocs. Retrieved from pillow.readthedocs.io/en/latest/handbook/index

[3] Lundh, F. (1995-2011), Clark, J. A. (2010-2023), and contributors.

Pillow (PIL Fork) 10.0.1 documentation – Tutorial. readthedocs. Retrieved from pillow.readthedocs.io/en/stable/handbook/ tutorial.html

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And NumPy Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Generative Adversarial Networks (GANs) - The Engine behind DALL-E

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

Leave a Reply

Your email address will not be published. Required fields are marked *