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

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Learn To Build A Python GUI For Easily Processing Images With Pillow Library In A Delphi Windows App
February 26, 2021 By Muhammad Azizul Hakim

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 Для просмотра ссылки Войди или Зарегистрируйся and Для просмотра ссылки Войди или Зарегистрируйся library, inside Для просмотра ссылки Войди или Зарегистрируйся. 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 Для просмотра ссылки Войди или Зарегистрируйся. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this Для просмотра ссылки Войди или Зарегистрируйся.

1614368042189.png
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:
Python:
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:
1614368076743.png

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:
Python:
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:
1614368103406.png
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:

Python:
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:
1614368140069.png
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:
Python:
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:
1614368179759.png
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: Для просмотра ссылки Войди или Зарегистрируйся and

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: Для просмотра ссылки Войди или Зарегистрируйся