Shaped window from transparent part of bitmap

I have a bitmap with a transparent part. This bitmap is painted multiple times in the frame. How can i use the transparent part of the bitmap to build a shape for the window. I’ve attached a sample that should demonstrate what i want to achieve. The sample uses given values for the sahpe, but my final code doesn’t know them.

shaped.py (6.53 KB)

Hi Torsten,

For additional clarification, it appears to me you wish to create a fixed width border around an arbitrary

graphic shape in Python (which has been provided to you in a bitmap surrounded by a transparent area).

Like coating a strawberry with chocolate and looking at the cross-section, you want an algorithm to

generate that chocolate area.

Is that the idea?

Rufus

···

On Sep 10, 2017, at 9:33 AM, ‘Torsten’ via wxPython-users wxpython-users@googlegroups.com wrote:

I have a bitmap with a transparent part. This bitmap is painted multiple times in the frame. How can i use the transparent part of the bitmap to build a shape for the window. I’ve attached a sample that should demonstrate what i want to achieve. The sample uses given values for the sahpe, but my final code doesn’t know them.


You received this message because you are subscribed to the Google Groups “wxPython-users” group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<shaped.py>

Hello Rufus,

no, i want a wx.Frame() that has no border and no background (like totally transparent), but only the frame. Buttons and pictures that i place inside should be fully visible. From my reading this is done with wx.frame.SetShape(wx.RegionFromBitmap(bmp)). The bmp is a black/white bitmap. The black part will be the transparent part of the frame (shape). For buttons i can easily get the position and size and paint a white rectangle on black ground. But the picture is my problem.

I have this:
Auto Generated Inline Image 1.png

And i want that:
Auto Generated Inline Image 2.png

What in the initial picture is transparent should be black, all other colours should be white.

I hope now is clear what goal is. :slight_smile:

···


Torsten

Do you actually need a circle? or that a simple example? If circle, then
you can easily do that by makign a black rectangle and drawing a white
circle on it with a wx.MemoryDC.

But I suspect that you have an "arbitrary" image.

wxBitmap has essentiually no functionality, but wxImage does have some
basic Image processing -- maybe:

wxImage.Replace() can help.

Or, for more robust functionality, use the PIL or numpy. -- you can convert
to/from PIL images or numpy arrays pretty efficiently:

https://wiki.wxpython.org/WorkingWithImages

(kind old page, but I hope most of it works)

-CHB

···

On Mon, Sep 11, 2017 at 10:18 AM, 'Torsten' via wxPython-users < wxpython-users@googlegroups.com> wrote:

no, i want a wx.Frame() that has no border and no background (like
totally transparent), but only the frame. Buttons and pictures that i place
inside should be fully visible. From my reading this is done with
wx.frame.SetShape(wx.RegionFromBitmap(bmp)). The bmp is a black/white
bitmap. The black part will be the transparent part of the frame (shape).
For buttons i can easily get the position and size and paint a white
rectangle on black ground. But the picture is my problem.

I have this:

And i want that:

What in the initial picture is transparent should be black, all other
colours should be white.

--

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.Barker@noaa.gov

Yes, the circle is just an example. It can be a more ‘complex’ figure. I’ll take a look at PIL. Thanks.

IIUC, one approach that comes to mind is to get the region from the original bitmap, create a new bitmap of the correct size and set it into a MemoryDC. Then clear the DC with a black brush, then do SetDeviceClippingRegion with the original bitmap’s transparent region, and then clear the DC with white.

If you need to combine multiple source bitmaps and get the remaining transparent space as a region, then another approach would be to fill a target bitmap & MemoryDC with a color you know is not in any of the sources, draw the sources to the bitmap, and then use SetMaskColour to set the target bitmap’s mask to include every pixel with that colour. Then you can use that bitmap to extract the region to be used to set the frame’s shape.

···

On Monday, September 11, 2017 at 10:18:45 AM UTC-7, Torsten wrote:

Hello Rufus,

no, i want a wx.Frame() that has no border and no background (like totally transparent), but only the frame. Buttons and pictures that i place inside should be fully visible. From my reading this is done with wx.frame.SetShape(wx.RegionFromBitmap(bmp)). The bmp is a black/white bitmap. The black part will be the transparent part of the frame (shape). For buttons i can easily get the position and size and paint a white rectangle on black ground. But the picture is my problem.

What in the initial picture is transparent should be black, all other colours should be white.


Robin Dunn

Software Craftsman

Hello Rufus,

no, i want a wx.Frame() that has no border and no background (like totally transparent), but only the frame. Buttons and pictures that i place inside should be fully visible. From my reading this is done with wx.frame.SetShape(wx.RegionFromBitmap(bmp)). The bmp is a black/white bitmap. The black part will be the transparent part of the frame (shape). For buttons i can easily get the position and size and paint a white rectangle on black ground. But the picture is my problem.

What in the initial picture is transparent should be black, all other colours should be white.

I was curious about this technique, because Iiked the effect, even in your demo.

It looks like numpy (found by Google, not by experience) might have functions you could use:

http://answers.opencv.org/question/97416/replace-a-range-of-colors-with-a-specific-color-in-python/

The answers/comments here don’t provide an exact solution, but seem to be on the right track.

···

On Monday, September 11, 2017 at 10:18:45 AM UTC-7, Torsten wrote: