python - 如何将静态位图帧从 GIF 馈送到 wxpython 形状的帧
问题描述
我正在努力弄清楚如何从用户选择的 GIF(具有透明背景)提交位图帧并显示 GIF/帧,而无需创建额外的 UI 元素来消除透明度。(假设用户的桌面环境不支持透明窗口)。
这里提供了最接近的解决方案:How to blit a image directly to the screen without a window?
但是,此解决方案仅支持单个图像。我正在尝试修改此处找到的代码以将 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毫秒),在到达最后一帧时循环回到开头。
这是使用 a 的经典案例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()
推荐阅读
- php - LARAVEL:MYSQL:完整性约束违规:1452 无法添加或更新子行:外键约束失败
- javascript - 外部 Javascript 仅在 Internet Explorer 上未定义
- sql - 如何提高 10M+ 记录表中的选择查询性能?
- ansible - set-fact 一个带有来自特定键的dict值的var
- sapui5 - UI5 - 为 UI5 配置 Karma 配置文件时遇到问题
- ruby-on-rails - 如何修复:错误:'
' 没有检查出致命的提交:输入“git add”时添加文件失败。在命令提示符下 - sql-server - Azure PowerShell Invoke-Sqlcmd:将数据类型 nvarchar 转换为 uniqueidentifier 时出错
- java - 保存在本地文件夹后如何使用 Spring Boot 恢复图像
- python - 如何在烧瓶 Web 应用程序中显示分类报告
- java - 在 Java 中是否可以同时返回枚举名称和值?