2010-07-19 66 views
1

我已经隔离了问题的原因是图像,因为代码似乎与透明度的其他PNG图像一起工作。但是,它似乎不适用于我需要的一个图像。当我试图制作一个不错的窗口时,这将非常有帮助。为什么这段代码不接受这个PNG图像的透明度?

图像:

alt text http://i32.tinypic.com/f1xlj7.png

的代码:

import wx 

class PictureWindow(wx.Frame): 
    def __init__(self, parent, id, title, pic_location): 

     # For PNGs. Must be PNG-8 for transparency... 
     self.bmp = wx.Image(pic_location, wx.BITMAP_TYPE_PNG).ConvertToBitmap() 

     framesize = (self.bmp.GetWidth(), self.bmp.GetHeight()) 

     # Launch a frame the size of our image. Note the position and style stuff... 
     # (Set pos to (-1, -1) to let the OS place it. 
     # This style wx.FRAME_SHAPED is a frameless plain window. 
     wx.Frame.__init__(self, parent, id, title, size=framesize, pos = (50, 50), style = wx.FRAME_SHAPED) 

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

     # Define the panel and place the pic 
     panel = wx.Panel(self, -1) 
     self.mainPic = wx.StaticBitmap(panel, -1, self.bmp) 

     # Set an icon for the window if we'd like 
     #icon1 = wx.Icon("icon.ico", wx.BITMAP_TYPE_ICO) 
     #self.SetIcon(icon1) 

     self.Show() 

     # The paint stuff is only necessary if doing a shaped window 
     self.Bind(wx.EVT_PAINT, self.OnPaint) 
     self.Main() 


    def OnPaint(self, event): 
     dc = wx.PaintDC(self) 
     dc.DrawBitmap(self.bmp, 0, 0, True) 

    def Main(self): 
     sizer = wx.GridBagSizer() 
     button = wx.Button(self,-1,label="Click me !") 
     sizer.Add(button, (0,1)) 


# What pic are we opening? 
pic_location = r"C:\Users\user\Pictures\CPUBAR\A4.png" # PNG must be png-8 for    transparency... 

app = wx.App(redirect=0) # the redirect parameter keeps stdout from opening a wx gui window 
PictureWindow(None, -1, 'Picture Viewer', pic_location) 
app.MainLoop() 

这是Windows 7,顺便说一句。

+0

浪费了相当多的时间,但找不到差异,在瘸腿如果我触摸工作图像,并保存它们不工作后...这可能是线索 – 2010-07-19 08:30:21

+0

你可以直接加载bmp self.bmp = wx.Bitmap(pic_location,wx.BITMAP_TYPE_PNG) – 2010-07-19 08:30:39

回答

7

wx.RegionFromBitmap使用位图的掩码来设置形状。如果你的源图像有一个alpha通道,那么它将不会有掩码,所以wx.RegionFromBitmap将无法确定使用什么形状。您可以转换源图像,使所有像素完全不透明或完全透明,然后wx.Image将使用蒙版而不是Alpha通道加载它。或者,您可以在运行时使用wx.Image的ConvertAlphaToMask方法将其转换为wx.Bitmap。

+1

+1 - 哦罗宾邓恩在这里:) – 2010-07-20 03:09:38

0

你的代码表明它需要使用png-8格式才能使透明工作。 首先,是在png-8格式的图像? 秒,为什么这是透明度的必要条件?

+0

是的,我不确定。 – rectangletangle 2010-07-19 08:21:53

0

您正在将图像转换为位图 - 位图不支持透明度。

+1

谁这么说?我在我的代码中使用透明位图。 – 2010-07-20 03:07:03

0

作为一种解决方法,您可以使用.gif(如果您可以忍受有限的颜色设置)。 它们只支持1位的alpha通道。