Overlay bitmap as semi-transparent

342 views
Skip to first unread message

michael h

unread,
Aug 26, 2009, 4:22:07 PM8/26/09
to wxPython-users
Can someone point me in the right direction? I'm trying to overlay a
bitmap as semi-transparent onto another bitmap.

I simply want the whole bitmap to be transparent...I know I'm missing
something really basic here.

I've been looking at device/graphicscontext docs for a while with no
revelations...

Anyone have any suggestions for me?

- Michael

Robin Dunn

unread,
Aug 26, 2009, 7:33:34 PM8/26/09
to wxpytho...@googlegroups.com
michael h wrote:
> Can someone point me in the right direction? I'm trying to overlay a
> bitmap as semi-transparent onto another bitmap.

Assuming that the second bitmap already has the transparency set by
virtue of it being there in the original image file: Create a new
bitmap with wx.EmptyBitmap, and select it into wx.MemoryDC. Then do a
DrawBitmap with bitmapA and a DrawBitmap with bitmapB, making sure you
pass True for the transparent parameter. Then del the dc and the new
bitmap will now be a combination of the two originals.

--
Robin Dunn
Software Craftsman
http://wxPython.org

michael h

unread,
Aug 26, 2009, 7:59:52 PM8/26/09
to wxPython-users
The starting bitmaps do not have any transparency, I was hoping that
there was some way to draw all the pixels for the top bitmap at
partial transparency..

I'm working with an RGB triplet string, I thought maybe I could add
the alpha byte into it somehow (0x80=50% transparency?) and then use
BitmapFromBufferRGBA ...but all the ways i've tried doing that haven't
worked

I'm afraid my exposure to this type of thing has been fairly limited
up until now..

as a fallback PIL has an Image.blend function which I believe will
work, although I was hoping to avoid using it.

any advice is sincerely appreciated,

Thanks Robin!

Robin Dunn

unread,
Aug 26, 2009, 8:37:55 PM8/26/09
to wxpytho...@googlegroups.com
michael h wrote:
> The starting bitmaps do not have any transparency, I was hoping that
> there was some way to draw all the pixels for the top bitmap at
> partial transparency..
>
> I'm working with an RGB triplet string, I thought maybe I could add
> the alpha byte into it somehow (0x80=50% transparency?) and then use
> BitmapFromBufferRGBA ...but all the ways i've tried doing that haven't
> worked
>
> I'm afraid my exposure to this type of thing has been fairly limited
> up until now..
>
> as a fallback PIL has an Image.blend function which I believe will
> work, although I was hoping to avoid using it.
>
> any advice is sincerely appreciated,
>

Somebody familiar with numpy could probably give you three lines of code
to transmorgify your triplets into a numpy array of RGBA data. But
numpy is still mostly magic for me so I'll just point you to the
MakeBitmapRGBpA function in BitmapFromBuffer.py in the demo. Notice how
it is making a separate buffer for the alpha channel values? You should
be able to do the same thing to create a bitmap with alpha using your
existing RGB values.

michael h

unread,
Aug 26, 2009, 8:55:14 PM8/26/09
to wxPython-users
Yes, this is the conclusion I came to right before I started banging
my head against the wall...
I know how you feel regarding numpy...

I thought maybe there was some way to use one of the mask functions to
accomplish this effect as well...but I haven't been produce it..

thanks again

- michael

Christopher Barker

unread,
Sep 1, 2009, 3:58:42 PM9/1/09
to wxpytho...@googlegroups.com
Robin Dunn wrote:
> Somebody familiar with numpy could probably give you three lines of code
> to transmorgify your triplets into a numpy array of RGBA data.

challenge accepted:


import numpy as np

def MakeBitmapRGBAnumpy(self, width, height, RGBdata, alpha=255):
# Make an RGBA bitmap using RGB bytes
# passed in as a string or other buffer object.
# it should be width*height*3 bytes long.
# The alpha channel is added with value provided

# create the empty array
rgba = np.empty((width, height, 4), dtype=np.uint8)

#make an array from the input rgb buffer
rgb = np.frombuffer()
# copy the
rgba[:,:,:3] = rbg
rgba[:,:,3] = alpha

return = wx.wx.BitmapFromBufferRGBA(width, height, rgba)

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov

Reply all
Reply to author
Forward
0 new messages