def PilImageToWxImage(pilImage, copyAlpha=True): """Convert PIL image to wx.Image Based on http://wiki.wxpython.org/WorkingWithImages """ from gui_core.wrap import EmptyImage hasAlpha = pilImage.mode[-1] == "A" if copyAlpha and hasAlpha: # Make sure there is an alpha layer copy. wxImage = EmptyImage(*pilImage.size) pilImageCopyRGBA = pilImage.copy() pilImageCopyRGB = pilImageCopyRGBA.convert("RGB") # RGBA --> RGB wxImage.SetData(pilImageCopyRGB.tobytes()) # Create layer and insert alpha values. if wxPythonPhoenix: wxImage.SetAlpha(pilImageCopyRGBA.tobytes()[3::4]) else: wxImage.SetAlphaData(pilImageCopyRGBA.tobytes()[3::4]) else: # The resulting image will not have alpha. wxImage = EmptyImage(*pilImage.size) pilImageCopy = pilImage.copy() # Discard any alpha from the PIL image. pilImageCopyRGB = pilImageCopy.convert("RGB") wxImage.SetData(pilImageCopyRGB.tobytes()) return wxImage
def PilImageToWxImage(pilImage, copyAlpha=True): """Convert PIL image to wx.Image Based on http://wiki.wxpython.org/WorkingWithImages """ from gui_core.wrap import EmptyImage hasAlpha = pilImage.mode[-1] == 'A' if copyAlpha and hasAlpha: # Make sure there is an alpha layer copy. wxImage = EmptyImage(*pilImage.size) pilImageCopyRGBA = pilImage.copy() pilImageCopyRGB = pilImageCopyRGBA.convert('RGB') # RGBA --> RGB fn = getattr(pilImageCopyRGB, "tobytes", getattr(pilImageCopyRGB, "tostring")) pilImageRgbData = fn() wxImage.SetData(pilImageRgbData) fn = getattr(pilImageCopyRGBA, "tobytes", getattr(pilImageCopyRGBA, "tostring")) # Create layer and insert alpha values. if globalvar.wxPythonPhoenix: wxImage.SetAlpha(fn()[3::4]) else: wxImage.SetAlphaData(fn()[3::4]) else: # The resulting image will not have alpha. wxImage = EmptyImage(*pilImage.size) pilImageCopy = pilImage.copy() # Discard any alpha from the PIL image. pilImageCopyRGB = pilImageCopy.convert('RGB') fn = getattr(pilImageCopyRGB, "tobytes", getattr(pilImageCopyRGB, "tostring")) pilImageRgbData = fn() wxImage.SetData(pilImageRgbData) return wxImage