Masked bitmap from bitmap

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gandalf

    Masked bitmap from bitmap

    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):
    [color=blue]
    >wxBITMAP_TYPE_ ICO Load a Windows icon file.
    >wxBITMAP_TYPE_ ICO_RESOURCE Load a Windows icon from the resource[/color]
    database.[color=blue]
    >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[/color]
    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.CopyFrom Bitmap.

    Is not okay. This is from the docs:
    [color=blue]
    >Copies bmp bitmap to this icon. Under MS Windows the bitmap must have[/color]
    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



Working...