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 Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene 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:

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 250×167 (the previous image size is 1080×720).

 

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

close
Related posts
DelphiDelphiVCLLearn PythonPythonPython GUIWindows

Decipher the DelphiVCL.Application

DelphiDelphiVCLLearn PythonPythonPython GUIWindows

Create a Very Basic Empty Form using DelphiVCL.Form

DelphiDelphiVCLLearn PythonPythonPython GUIWindows

An Introduction To DelphiVCL: A Python GUI Builder For Windows

Learn PythonPythonPython GUI

Learn Python With Desktop GUI App With Python and Tkinter Video

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

jaJapanese