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.
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.
Table of Contents
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
:
1 2 3 4 5 6 |
from __future__ import print_function from PIL import Image im = Image.open("C:/Users/ASUS/cat1.jpg") print(im.format, im.size, im.mode) im.show() |
Run the script above in Demo01
GUI, we will get the following results:
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:
1 2 3 4 5 6 7 8 9 10 11 |
from __future__ import print_function from PIL import Image import os path = "C:/Users/ASUS/cat1.jpg" im = Image.open(path) size = (250, 250) outfile = os.path.splitext(path)[0] + "Resized.jpg" im.thumbnail(size) im.save(outfile, "JPEG") |
Run the script above, we will get the following result:
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:
1 2 3 4 5 6 7 8 9 10 11 |
from __future__ import print_function from PIL import Image im = Image.open("C:/Users/ASUS/cat1.jpg") box = (0, 0, 540, 720) region = im.crop(box) region = region.transpose(Image.ROTATE_180) region = region.transpose(Image.FLIP_LEFT_RIGHT) im.paste(region, box) im = im.rotate(45) im.save("C:/Users/ASUS/cat1Rotated.jpg") |
Run the script above, we will get the following result:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from PIL import Image img = Image.open("C:/Users/ASUS/cat1.jpg") img = img.convert("RGB") datas = img.getdata() new_image_data = [] for item in datas: # Change all white (also shades of whites) pixels to yellow if item[0] in list(range(190, 256)): new_image_data.append((255, 204, 100)) else: new_image_data.append(item) # Update image data img.putdata(new_image_data) # Save new image img.save("cat1_alteredBackground.jpg") # Show image in preview img.show() |
Let’s see this interesting result:
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