#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    27
    Rep Power
    0

    Smile wxPython Transparency Hack


    I have been using wxPython in a program I am developing, and have tried to draw various transparent images in the process. I was quite disturbed by the rendering of the transparent areas, especially with brighter colors. [Note: This error occurs specifically when using the DrawBitmap method of the wx.DC class. Rendering seems fine using the wx.StaticBitmap object.]

    The attached image is a compilation of 6 colors. Each square goes from Black to $Color (left to right) and 100% to 0% opacity (top to bottom). The left column of squares shows the default transparency rendering of wxPython. The right column of squares represents what things should look like. Notice how the background colors get extremely distorted as the color intensifies.

    After much investigation and tinkering, I discovered a simple fix to the problem. I won't detail here why it works, but it does. The solution is to multiply the RGB value of each pixel by the percent transparency desired for that pixel (where 0 <= percent <= 1). Then simply display the image with the modified color values with the original alpha values.

    Code:
    RedVal = pcnt * RedVal
    GreenVal = pcnt * GreenVal
    BlueVal = pcnt * BlueVal
    Simple no? The problem comes when you are trying to examine every pixel value and modify it. For any considerable sized image, this could really bog your code down.

    I've created a C extension to handle this very performance issue. I've also created a simple Python module to process existing images and to generate transparent bitmaps of a given color and percent transparency. If this has been your frustration, feel free to email me, and I'll get you set up.

    Hope this helps someone out there! And if you know of a better way, please do share!

    Derrick
    Attached Images
    Last edited by p4j; August 29th, 2004 at 07:40 PM. Reason: Error when using wx.DC.DrawBitmap
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Very late reply


    This is quite an epicly late reply, but if you still have those C and python modules somewhere that would be very useful. Please do get in touch if so!

IMN logo majestic logo threadwatch logo seochat tools logo