0
此代碼在我的Ubuntu系統上完美工作,但是當我在Windows(7 x64型)上運行它時,圖像不顯示。在Windows中顯示PIL圖像在wx python中
此代碼是一個比較複雜的系統,需要枕頭的圖像平滑組合成所需的格式
誰能幫助請的一部分嗎?我可能會缺少依賴或什麼?
import wx
from PIL import Image
class Example(wx.Frame):
"""
class description
"""
def __init__(self):
"""
initialise form
"""
wx.Frame.__init__(self, None, -1, 'Title', size=(600, 600))
sizer = wx.BoxSizer(wx.HORIZONTAL)
bitmap = wx.Bitmap('circle.png', wx.BITMAP_TYPE_PNG)
image = bitmap.ConvertToImage()
image = self.image_to_pil(image)
static_bitmap = self.static_bitmap_from_pil_image(self, image)
sizer.Add(static_bitmap)
self.SetSizer(sizer)
self.Centre()
self.Layout()
@staticmethod
def static_bitmap_from_pil_image(caller, pil_image):
wx_image = wx.EmptyImage(pil_image.size[0], pil_image.size[1])
wx_image.SetData(pil_image.convert("RGB").tobytes())
wx_image.SetAlphaData(pil_image.convert("RGBA").tobytes()[3::4])
bitmap = wx.BitmapFromImage(wx_image)
static_bitmap = wx.StaticBitmap(caller, wx.ID_ANY, wx.NullBitmap)
static_bitmap.SetBitmap(bitmap)
return static_bitmap
@staticmethod
def image_to_pil(image):
"""Convert wx.Image to PIL Image."""
width, height = image.GetSize()
data = image.GetData()
red_image = Image.new("L", (width, height))
red_image.frombytes(data[0::3])
green_image = Image.new("L", (width, height))
green_image.frombytes(data[1::3])
blue_image = Image.new("L", (width, height))
blue_image.frombytes(data[2::3])
if image.HasAlpha():
alpha_image = Image.new("L", (width, height))
alpha_image.frombytes(image.GetAlphaData())
pil_image = Image.merge('RGBA', (red_image, green_image, blue_image, alpha_image))
else:
pil_image = Image.merge('RGB', (red_image, green_image, blue_image))
return pil_image
if __name__ == '__main__':
"""
initialise application
"""
simple_screen_app = wx.App()
main_frame = Example()
main_frame.Show(True)
simple_screen_app.MainLoop()
作爲回答的線必須在某些情況下一些意義,我的修正不會有普遍的,我不會慶祝這一適用性 – Psionman
源圖像是否有alpha通道? – RobinDunn
羅賓。我所做的測試不包括一個alpha通道,但顯然其他圖像可能(並且實際上將包含)一個alpha層。有關這可能意味着什麼? – Psionman