Image Transparency Color in FireMonkey (FMX) with Python

87 Views Asked by At

I know that the Image component support transparency in the FMX library with Python as long as the image has transparency built into the image, but what if you want to set a certain color to be the transparency color?

As an example. Let's say I have a PNG Image and I want all white ($FFFFFFFF) colors to be transparent, is this possible to do? If yes, then how?

In short, I've created a basic app with a PNG Image on it that has white in it. I want all the white in the image to be transparent. Here's a screenshot of my app:

Python GUI App with Image

The white part around the circle is supposed to be transparent. I want to set that white color as the transparent color.

It seems like it's definitely possible in the FireMonkey (FMX) framework if you're using C++ Builder or Delphi due to this question and answer. Looks like it can also be done in design time using the Delphi or C++ Builder IDE from this answer.

If I do this during design time in a Delphi IDE, then it works fine. I can then also check the .fmx code for it and I see there's this line: MultiResBitmap.TransparentColor = 16777215. Here's the full .fmx code and IDE screenshot for that:

Delphi IDE Image

object MyImg: TImage
    MultiResBitmap.Height = 55
    MultiResBitmap.Width = 70
    MultiResBitmap.TransparentColor = 16777215
    MultiResBitmap = <
        item
        Width = 70
        Height = 55
        PNG = {
            89504E470D0A1A0A0000000D4948445200000046000000370806000000B1503C
            E0000000017352474200AECE1CE90000000467414D410000B18F0BFC61050000
            0110494441546843EDD7410E8320104661EDF13C30D76BC16842090FAB8185F5
            7D1B1B8BB3F803E3384FD1B22CEF744D4208EBBDA7293398F31BBBA78553CBA0
            1A8CA6E9B55D553018608F896A19B86380C100830106039C63803B06180C708E
            899C634E30186030C06080730C70C7008301CE319173CC0906030C06180C708E
            01EE186030C060C04F3DA61CF88E9EB9FBFAA4194C59B0543E7BF7F5393C4A47
            45AF185133D7B3BE3D06180C301880C1B41AD355236AE67AD6F7750DFC5602D5
            A35426DC92D6DE7DFDF6F38BCD17789480CD17F8AD04B0C71C15BD6244CD5CCF
            FA365F6030C0600006D36A4C578DA899EB59DFD73570C003F6186030A07A94CA
            33F9EF6A19AC01E47F3C2D94DD770661FE00988EEB436D12D6BA000000004945
            4E44AE426082}
        FileName = 'C:\Development\Farm\Assets\Dam.png'
        end>
    Position.X = 1156.000000000000000000
    Position.Y = 1785.000000000000000000
    Size.Width = 64.000000000000000000
    Size.Height = 50.000000000000000000
    Size.PlatformDefault = False
end

But how is this done in Python via code during runtime?

Currently, I am loading the Image by going self.myImg.Bitmap.LoadFromFile(). Maybe there is a property in Bitmap or a way to set this transparency color?

Here's my full Python code:

from delphifmx import *
import os

class frmMain(Form):
    def __init__(self, owner):
        self.Caption = 'My Form with Transparent Image'
        self.Width = 800
        self.Height = 500

        self.myImg = Image(self)
        self.myImg.Parent = self
        self.myImg.Position.X = 100
        self.myImg.Position.Y = 100
        self.myImg.Width = 300
        self.myImg.Height = 300

        path = os.path.dirname(os.path.abspath(__file__))
        self.myImg.Bitmap.LoadFromFile(path + '\Win64\Debug\Assets\Dam.png')

def main():
    Application.Initialize()
    Application.Title = "My Application"
    Application.MainForm = frmMain(Application)
    Application.MainForm.Show()
    Application.Run()
    Application.MainForm.Destroy()

main()

I also tried doing it the way it seems like it is done in the .fmx code by using the MultiResBitmap Bitmap instead of normal Bitmap, but that doesn't work either:

path = os.path.dirname(os.path.abspath(__file__))
self.myImg.MultiResBitmap.TransparentColor = 16777215
self.myImg.MultiResBitmap[0].Bitmap.LoadFromFile(path + '\Win64\Debug\Assets\Dam.png')
0

There are 0 best solutions below