August 26th, 2004, 04:13 PM
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.
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.
RedVal = pcnt * RedVal
GreenVal = pcnt * GreenVal
BlueVal = pcnt * BlueVal
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!
Last edited by p4j; August 29th, 2004 at 08:40 PM.
Reason: Error when using wx.DC.DrawBitmap
January 30th, 2013, 02:45 PM
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!