2010-07-19 24 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通道。