我正在努力弄清楚如何从用户选择的GIF(具有透明背景)提交位图框架并显示GIF /框架,而无需创建其他会删除透明度的UI元素。 (假设用户的桌面环境不支持透明窗口)。
此处提供了最接近的解决方案:How to blit a image directly to the screen without a window?
但是,此解决方案仅支持单个图像。我试图修改找到的here代码,以将GIF转换为帧,然后强制wxPython显示每个帧,但是我完全迷失了如何对帧显示进行计时,或者这甚至是正确的方法。我已经在下面复制了我正在使用的代码。
import wx
from PIL import Image, ImageSequence
IMAGE_PATH = 'TEST.gif'
im = Image.open(IMAGE_PATH)
index = 1
for frame in ImageSequence.Iterator(im):
frame.save("frame%d.png" % index)
index += 1
class ShapedFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Shaped Window",
style = wx.FRAME_SHAPED | wx.SIMPLE_BORDER)
self.hasShape = False
self.delta = wx.Point(0,0)
self.bmp = wx.BitmapFromImage(image)
self.SetClientSize((self.bmp.GetWidth(), self.bmp.GetHeight()))
dc = wx.ClientDC(self)
dc.DrawBitmap(self.bmp, 0,0, True)
self.SetWindowShape()
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.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
def OnEraseBackground(self,evt=None):
pass
def SetWindowShape(self, evt=None):
r = wx.RegionFromBitmap(self.bmp)
self.hasShape = self.SetShape(r)
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()
pos = self.ClientToScreen(evt.GetPosition())
origin = self.GetPosition()
self.delta = wx.Point(pos.x - origin.x, pos.y - origin.y)
def OnMouseMove(self, evt):
if evt.Dragging() and evt.LeftIsDown():
pos = self.ClientToScreen(evt.GetPosition())
newPos = (pos.x - self.delta.x, pos.y - self.delta.y)
self.Move(newPos)
def OnLeftUp(self, evt):
if self.HasCapture():
self.ReleaseMouse()
i = 0
if __name__ == '__main__':
while 1:
while i >= index:
# Load the image
img = 'frame%s.png' % (i)
image = wx.Image(img, wx.BITMAP_TYPE_PNG)
app = wx.PySimpleApp()
ShapedFrame().Show()
app.MainLoop()
i = i + 1
答案 0 :(得分:0)
我不确定您想通过“ shapeframe”业务实现什么目标,但这应该为您指明正确的方向。
我正在用gif
文件中的帧填充屏幕,每1/2秒(500毫秒)更改一次帧,并在到达最后一帧时循环回到起点。
这是使用wx.Timer
的经典案例。
显然,如果不需要Scale
例程,则会丢失它。
import wx
from PIL import Image, ImageSequence
IMAGE_PATH = 'TEST.gif'
im = Image.open(IMAGE_PATH)
imgs=[]
index = 1
for frame in ImageSequence.Iterator(im):
frame.save("frame%d.png" % index)
imgs.append("frame%d.png" % index)
index += 1
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Gif Frames")
self.screenW = wx.SystemSettings.GetMetric( wx.SYS_SCREEN_X )
self.screenH = wx.SystemSettings.GetMetric( wx.SYS_SCREEN_Y )
self.SetSize(self.screenW,self.screenH)
self.image_counter = 0
image = wx.Image(imgs[self.image_counter], wx.BITMAP_TYPE_PNG)
self.size = image.GetSize()
bitmap = self.Scale(image)
self.img = wx.StaticBitmap(self, wx.ID_ANY, bitmap)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.Show()
self.timer.Start(500)
def Scale(self,image):
image = image.Scale(self.screenW,self.screenH, wx.IMAGE_QUALITY_HIGH)
#image = image.Scale(self.size[0],self.size[1], wx.IMAGE_QUALITY_HIGH)
result = wx.Bitmap(image)
return result
def OnTimer(self, event):
#New frame every 1/2 a second
self.image_counter +=1
if self.image_counter > len(imgs) -1:
self.image_counter = 0
image = wx.Image(imgs[self.image_counter], wx.BITMAP_TYPE_PNG)
bitmap = self.Scale(image)
self.img.SetBitmap(bitmap)
def OnExit(self, evt):
self.Destroy()
if __name__ == '__main__':
app = wx.App()
MyFrame()
app.MainLoop()