Ask for your help about shapewindow demo

Hi All,

I used the shapedwindow in wxpython to show my picture, but the the picture frame appeared dirty.

Anyone can tell me what’s my problem?

The codes I changed in the shapewindow demo:

self.bmp = images.Vippi.GetBitmap()

To

self.bmp = wx.Image(opj(‘bitmaps/test/xianshi6.png’), wx.BITMAP_TYPE_PNG).ConvertToBitmap()

self.bmp.SetMask(wx.Mask(self.bmp, wx.BLACK))

The attached files are the picture files:

xianshi6.png – a boy I drawed it in photoshop, and saved as “png”

xianshi5.png – I used it to test the black background,it also has the same problem

compare.jpg – it’s the contrast
picture.

what can I do to make the frame just like what I have drawn in PhotoShop?

Thanks!

usr root

xianshi6.png

compare.jpg

xianshi5.png

···

Hi usr root,

I think what you’re looking for is:

window.SetShape(wx.RegionFromBitmap(self.bmp))

though, I use this call on a wx.Frame in my app as opposed to a ShapedWindow, so I can’t guarantee it’ll work for you.

-Mike

···

On Thu, Sep 3, 2009 at 9:53 PM, usr.root usr.root@gmail.com wrote:

Hi All,

I used the shapedwindow in wxpython to show my picture, but the the picture frame appeared dirty.

Anyone can tell me what’s my problem?

The codes I changed in the shapewindow demo:

self.bmp = images.Vippi.GetBitmap()

To

self.bmp = wx.Image(opj(‘bitmaps/test/xianshi6.png’), wx.BITMAP_TYPE_PNG).ConvertToBitmap()

self.bmp.SetMask(wx.Mask(self.bmp, wx.BLACK))

The attached files are the picture files:

xianshi6.png – a boy I drawed it in photoshop, and saved as “png”

xianshi5.png – I used it to test the black background,it also has the same problem

compare.jpg – it’s the contrast
picture.

what can I do to make the frame just like what I have drawn in PhotoShop?

Thanks!

usr root

Hi Mike,

Thanks. I am not sure about what does your “window” mean. In face, I changed the codes in the wxpython demo.

These are the changed codes: (The codes in red are what I have changed)

import wx

import images

from Main import opj

import ImageEnhance

import Image

···

#----------------------------------------------------------------------

class TestFrame(wx.Frame):

def init(self, parent, log):

self.log = log

wx.Frame.init(self, parent, -1, “Shaped Window”,

style =

wx.FRAME_SHAPED

wx.SIMPLE_BORDER

wx.FRAME_NO_TASKBAR

wx.STAY_ON_TOP

)

self.hasShape = False

self.delta = (0,0)

self.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick)

self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)

self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)

self.Bind(wx.EVT_MOTION, self.OnMouseMove)

self.Bind(wx.EVT_RIGHT_UP, self.OnExit)

self.Bind(wx.EVT_PAINT, self.OnPaint)

    self.bmp = wx.Image(opj('bitmaps/test/xianshi5.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap()

    self.bmp.SetMask(wx.Mask(self.bmp, wx.BLACK))

w, h = self.bmp.GetWidth(), self.bmp.GetHeight()

self.SetClientSize( (w, h) )

if wx.Platform != “WXMAC”:

wxMac clips the tooltip to the window shape, YUCK!!!

self.SetToolTipString(“Right-click to close the window\n”

“Double-click the image to set/unset the window shape”)

if wx.Platform == “WXGTK”:

wxGTK requires that the window be created before you can

set its shape, so delay the call to SetWindowShape until

this event.

self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)

else:

On wxMSW and wxMac the window has already been created, so go for it.

self.SetWindowShape()

dc = wx.ClientDC(self)

dc.DrawBitmap(self.bmp, 0,0, True)

def SetWindowShape(self, *evt):

Use the bitmap’s mask to determine the region

#r = wx.RegionFromBitmap(self.bmp)

#window.SetShape(wx.RegionFromBitmap(self.bmp))

self.hasShape = self.SetShape(wx.RegionFromBitmap(self.bmp))

def OnDoubleClick(self, evt):

if self.hasShape:

self.SetShape(wx.Region())

self.hasShape = False

else:

self.SetWindowShape()

def OnPaint(self, evt):

dc = wx.PaintDC(self)

dc.DrawBitmap(self.bmp, 0,0, True)

def OnExit(self, evt):

self.Close()

def OnLeftDown(self, evt):

self.CaptureMouse()

x, y = self.ClientToScreen(evt.GetPosition())

originx, originy = self.GetPosition()

dx = x - originx

dy = y - originy

self.delta = ((dx, dy))

def OnLeftUp(self, evt):

if self.HasCapture():

self.ReleaseMouse()

def OnMouseMove(self, evt):

if evt.Dragging() and evt.LeftIsDown():

x, y = self.ClientToScreen(evt.GetPosition())

fp = (x - self.delta[0], y - self.delta[1])

self.Move(fp)

#---------------------------------------------------------------------------

class TestPanel(wx.Panel):

def init(self, parent, log):

self.log = log

wx.Panel.init(self, parent, -1)

b = wx.Button(self, -1, “Show the ShapedWindow sample”, (50,50))

self.Bind(wx.EVT_BUTTON, self.OnButton, b)

def OnButton(self, evt):

win = TestFrame(self, self.log)

win.Show(True)

#---------------------------------------------------------------------------

def runTest(frame, nb, log):

win = TestPanel(nb, log)

return win

#----------------------------------------------------------------------

overview = “”"

Shaped Window

Top level windows now have a SetShape method that lets you set a

non-rectangular shape for the window using a wxRegion. All pixels

outside of the region will not be drawn and the window will not be

sensitive to the mouse in those areas either.

“”"

if name == ‘main’:

import sys,os

import run

run.main([‘’, os.path.basename(sys.argv[0])] + sys.argv[1:])

And my problem is:

The shapedwindow is ok for me, but it looks a little dirty, you can see this by picture “compare.jpg”

The left picture is PS picture, and the right picture is my shapedwindow.

As you know, the picture is ok if we open it with other photogram view tools.

Best Wishes,

Usr root


发件人: Michael Haimes

发送时间: 2009-09-04 14:41:26

收件人: wxpython-users

抄送:

主题: [wxPython-users] Re: Ask for your help about shapewindow demo

Hi usr root,
I think what you’re looking for is:

window.SetShape(wx.RegionFromBitmap(self.bmp))

though, I use this call on a wx.Frame in my app as opposed to a ShapedWindow, so I can’t guarantee it’ll work for you.

-Mike

On Thu, Sep 3, 2009 at 9:53 PM, usr.root usr.root@gmail.com wrote:

Hi All,

I used the shapedwindow in wxpython to show my picture, but the the picture frame appeared dirty.

Anyone can tell me what’s my problem?

The codes I changed in the shapewindow demo:

self.bmp = images.Vippi.GetBitmap()

To

self.bmp = wx.Image(opj(‘bitmaps/test/xianshi6.png’), wx.BITMAP_TYPE_PNG).ConvertToBitmap()

self.bmp.SetMask(wx.Mask(self.bmp, wx.BLACK))

The attached files are the picture files:

xianshi6.png – a boy I drawed it in photoshop, and saved as “png”

xianshi5.png – I used it to test the black background,it also has the same problem

compare.jpg – it’s the contrast picture.

what can I do to make the frame just like what I have drawn in PhotoShop?

Thanks!

usr root

usr.root wrote:

Hi All,
I used the shapedwindow in wxpython to show my picture, but the the picture frame appeared dirty.
Anyone can tell me what's my problem?
The codes I changed in the shapewindow demo:
self.bmp = images.Vippi.GetBitmap()
To
self.bmp = wx.Image(opj('bitmaps/test/xianshi6.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap()

self.bmp.SetMask(wx.Mask(self.bmp, wx.BLACK))
The attached files are the picture files:
xianshi6.png -- a boy I drawed it in photoshop, and saved as "png"
xianshi5.png -- I used it to test the black background,it also has the same problem
compare.jpg -- it's the contrast picture.
what can I do to make the frame just like what I have drawn in PhotoShop?

The problem is that your drawing is using anti-aliasing to blend the pixels along the edge of the drawing with the background, to make the lines appear to be smoother and more rounded. Those pixels are not fully black, and so when you set the mask using black then they are not included in the mask, and so still appear (as almost black) when you draw the image. This becomes more noticeable when the background you draw upon is further from black.

You can remedy this by not using anti-aliasing when drawing your source image, at least not along the outside edges. Then the mask will include all the pixels up to the edge of your drawing because they will be the same as the background and not blended with the foreground. However this will result in jagged edges around the curves and diagonal lines.

Another possibility is to still use anti-aliasing but use a lighter color (like a medium-light gray) for the background. Then the blended pixels along the edge will be more likely to look okay on many other backgrounds the image is drawn over, and even when it doesn't blend well it won't stand out as much as the black does.

Finally, you could continue to use anti-aliasing but incorporate some sore of border around the subject of your image and not use anti-aliasing there. That will look better and the straight edges won't end up being jagged.

···

--
Robin Dunn
Software Craftsman

Hi Robin,

Thank you!

Did you mean I should amend the picture’s border? Is there any way we do not change the picture, but do something in the programming codes?

Since our art designer always draw picture as there own way, I think it’s a hard work to ask them to amend the picture.

Thanks.

2009-09-07

···

usr.root


发件人: Robin Dunn

发送时间: 2009-09-05 01:29:44

收件人: wxpython-users

抄送:

主题: [wxPython-users] Re: Ask for your help about shapewindow demo

usr.root wrote:

Hi All,

I used the shapedwindow in wxpython to show my picture, but the

the picture frame appeared dirty.

Anyone can tell me what’s my problem?

The codes I changed in the shapewindow demo:

self.bmp = images.Vippi.GetBitmap()

To

self.bmp = wx.Image(opj(‘bitmaps/test/xianshi6.png’), wx.BITMAP_TYPE_PNG).ConvertToBitmap()

self.bmp.SetMask(wx.Mask(self.bmp, wx.BLACK))

The attached files are the picture files:

xianshi6.png – a boy I drawed it in photoshop, and saved as “png”

xianshi5.png – I used it to test the black background,it also has the

same problem

compare.jpg – it’s the contrast picture.

what can I do to make the frame just like what I have drawn in PhotoShop?

The problem is that your drawing is using anti-aliasing to blend the

pixels along the edge of the drawing with the background, to make the

lines appear to be smoother and more rounded. Those pixels are not

fully black, and so when you set the mask using black then they are not

included in the mask, and so still appear (as almost black) when you

draw the image. This becomes more noticeable when the background you

draw upon is further from black.

You can remedy this by not using anti-aliasing when drawing your source

image, at least not along the outside edges. Then the mask will include

all the pixels up to the edge of your drawing because they will be the

same as the background and not blended with the foreground. However

this will result in jagged edges around the curves and diagonal lines.

Another possibility is to still use anti-aliasing but use a lighter

color (like a medium-light gray) for the background. Then the blended

pixels along the edge will be more likely to look okay on many other

backgrounds the image is drawn over, and even when it doesn’t blend well

it won’t stand out as much as the black does.

Finally, you could continue to use anti-aliasing but incorporate some

sore of border around the subject of your image and not use

anti-aliasing there. That will look better and the straight edges won’t

end up being jagged.

Robin Dunn

Software Craftsman

usr.root wrote:

Hi Robin,
Thank you!
Did you mean I should amend the picture's border? Is there any way we do not change the picture, but do something in the programming codes?

Nothing easy that I can think of.

···

--
Robin Dunn
Software Craftsman