Masked bitmap from bitmap

G

Gandalf

Hi Gurus! Here is a problem with wxPython. I would like to load bitmaps
and create a mask
for them at once. Here is my idea: the mask colour for the bitmap should
be the colour of
the pixel in the top left corner. (The same way Delphi does with
TImageList.) E.g. the
bitmap should be transparent everywhere with the same colour. I read the
documentation,
and I could not find an easy way to do this. Here is what I have tried:

1. Create a wxIcon directly (which is a wxBitmap, I only need a masked
wxBitmap.)

This does not suit for me, since I need to load bitmaps on all
platforms. wxIcon cannot handle the BMP format on all platforms.
This is from the docs (about the type parameter of wx.Icon):
>wxBITMAP_TYPE_ICO Load a Windows icon file.
>wxBITMAP_TYPE_ICO_RESOURCE Load a Windows icon from the resource database.
>wxBITMAP_TYPE_GIF Load a GIF bitmap file.
>wxBITMAP_TYPE_XBM Load an X bitmap file.
>wxBITMAP_TYPE_XPM Load an XPM bitmap file.
>
>The validity of these flags depends on the platform and wxWindows
configuration. If all possible wxWindows settings are used, the Windows
platform supports ICO file, ICO resource, XPM data, and XPM file. Under
wxGTK, the available formats are BMP file, XPM data, XPM file, and PNG
file. Under wxMotif, the available formats are XBM data, XBM file, XPM
data, XPM file.

wxIcon cannot handle BMP on the Windows platform so I cannot use wxIcon.

2. Load wxBitmap and use wxIcon.CopyFromBitmap.

Is not okay. This is from the docs:
>Copies bmp bitmap to this icon. Under MS Windows the bitmap must have
mask colour set.

Back to the stove: once I have a bitmap with a mask, I do not really
need a wxIcon. :-( For most platforms, this solution would be fine but
not on M$ Windows.

3. Load a bitmap and specify the mask by hand. That would be great.
Unfortunately, I cannot get the colour of the top left pixel. There is
no such method, or I could not find it :-/ Even if there were one, I
would had created the mask by hand, in Python. I cannot set pixels'
colours (no method for that). And of course, this would be very slow.

4. Load the bitmap in PIL, create the mask by hand, save the mask into a
different file/object, load the bitmap and the saved mask into different
objects, finally combine them. Oh yeah, I can imagine this would work.
But it is too difficult and it would be very very very slow. There must
be a better way.

Please note that I do not want to use C/SWIG or any external tool. I
would like to use pure Python with wxPython if possible. Can somebody
help me?
A code fragment or a link would be great.

Thanks in advance,

Gandalf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top