Are you looking for a simple way to process images programmatically? You can do it with Python for Delphi using Pillow library. Python for Delphi (P4D) is a free tool that allows you to execute Python scripts, create new Python modules and types in Delphi. This post will guide you on how to run Pillow library code using Python for Delphi.
Delphi itself offers a number of advanced image manipulation capabilities through TImage in VCL and through TImage and the effects library in FMX If you have an existing Python application and you need to manipulate images you could use the Pillow library or you could send the image to Delphi and manipulate it there. Alternately, you could manipulate the image via Pillow and then display it in the Delphi Windows GUI app. You can use Python4Delphi a number of different ways such as:
With Pillow library, you can perform geometric and color transformations. It also allows to cut, copy part of the image and merge several images into one. Let’s take a look at some examples.
Now you can make various modifications with your images using Pillow library and Python for Delphi. Go
here to download Pillow library. Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.
- Create a Windows GUI around you existing Python app.
- Add Python scripting to your Delphi Windows apps.
- Add parallel processing to your Python apps through Delphi threads.
- Enhance your speed sensitive Python apps with functions from Delphi for more speed.
1 2 3 4 |
procedure TForm1.Button1Click(Sender: TObject); begin PythonEngine1.ExecStrings( Memo1.Lines ); end; |
Table of Contents
Open, show, and get image properties
First, open the image using function open(). You can get image properties such as format, size, type.
1 2 3 4 5 |
from __future__ import print_function from PIL import Image im = Image.open("test.jpg") print(im.format, im.size, im.mode) im.show() |
Create thumbnails
thumbnail() function allows you to create an image thumbnail. The input parameters of this function are the size of the image that you want to get in pixels. Use 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 = "test.JPG" im = Image.open(path) size = (250, 250) outfile = os.path.splitext(path)[0] + ".thumbnail" im.thumbnail(size) im.save(outfile, "JPG") |
Geometrical transformations
Function transpose() allows you to perform different geometrical transformations with the image. For example, you can rotate the image by a given angle or flip it horizontally and vertically.
1 2 3 4 5 6 7 8 9 10 |
from __future__ import print_function from PIL import Image im = Image.open("test.jpg") box = (0, 0, 320, 426) 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("test2.jpg") |
Change images colors
Now let’s look at how to change image color. Function split() allows you to decompose the image into separate colors and work with each color separately. In the following example first, we split the image into separate parts by color. Then select the area where the green value is less than 150. At the next step, we increase the blue value by 0.5. In the end, we merge everything into a new image.
1 2 3 4 5 6 7 |
source = im.split() R, G, B = 0, 1, 2 mask = source[G].point(lambda i: i < 150 and 255) out = source[B].point(lambda i: i * 0.5) source[R].paste(out, None, mask) source[B].paste(out, None, mask) im = Image.merge(im.mode, source) |
Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition