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
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!