gpt4 book ai didi

python - 如何将静态位图帧从 GIF 提供给 wxpython 形状帧

转载 作者:太空宇宙 更新时间:2023-11-03 20:59:17 25 4
gpt4 key购买 nike

我正在努力弄清楚如何从用户选择的 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

最佳答案

我不确定您想通过“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()

enter image description here

关于python - 如何将静态位图帧从 GIF 提供给 wxpython 形状帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55807143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com