By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,915 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,915 IT Pros & Developers. It's quick & easy.

Masked bitmap from bitmap

P: n/a
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
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

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,


Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.