One of the key features of Python4Delphi is the ability to use the existing Python Libraries in your Delphi/C++Builder Application with minimal code. How to import and use the existing Python Library in your Delphi application? This post will help to understand with a Python4Delphi Sample App which imports the Python Image Library(Pillow) and perform simple Image processing task quickly.
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:
- 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.
Python4Delphi Demo29 Sample App shows how to import a Python Image Library(PIL) and manipulate to rotate a simple image to 90 degrees using the module API. You can find the Demo29 source on GitHub.
Prerequisites: Download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out this video Getting started with Python4Delphi. You should install the Pillow using the command ‘pip install Pillow’. Check more about installing python packages here.
Components used in Python4Delphi Demo29 Sample App:
- TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class.
- TPythonGUIInputOutput: Inherited from TPythonInputOutput (which works as a console for python outputs) Using this component Output property you can associate the Memo component to show the Output.
- TImage: To display a graphical image on a form. Use the TPicture object in the Picture property to specify the actual bitmap, con, metafile, or other graphic object displayed by TImage.
- TMemo: A multiline text editing control, providing text scrolling. The text in the memo control can be edited as a whole or line by line.
You can find the Python4Delphi Demo29 sample project from the extracted repository ..Python4DelphiDemosDemo29.dproj. Open this project in RAD Studio 10.4.1 and run the application.
Find out why Python GUI is an ideal language for novices and experienced programmers alike.
Implementation Details:
- PythonEngine1 provides the connection to Python or rather the Python API. This project uses Python3.9 which can be seen in the TPythonEngine DllName property.
- PythonGUIInputOutput1 provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
executing Python script. - In this sample, the TImage component is used to display the selected image upon clicking ‘Open Picture..’ Button. On clicking the ‘Execute’ Button the below code is executed which in turn executes the python script mentioned in Memo1(where the process image to rotate 90 degrees is carried on).
The script used to process the image is shown below.