2017-05-07 89 views
1

因此,几个小时前我发现Pyglet最适合呈现gif动画,所以我',这是新的。我的问题是,动画gif在全屏窗口呈现其原始大小,我需要使它匹配,但我不知道我该怎么做,有什么帮助吗?我的代码:如何使用Pyglet在Python中使gif动画匹配全屏窗口大小?

import sys 

import pyglet 
from pyglet.window import Platform 

if len(sys.argv) > 1: 
    animation = pyglet.image.load_animation(sys.argv[1]) 
    bin = pyglet.image.atlas.TextureBin() 
    animation.add_to_texture_bin(bin) 
else: 
    animation = pyglet.resource.animation('gaben.gif') 
    sprite = pyglet.sprite.Sprite(animation) 

screen = Platform().get_default_display().get_default_screen() 
window = pyglet.window.Window(width=screen.width, height=screen.height) 
window.set_fullscreen(True) 

pyglet.gl.glClearColor(1, 1, 1, 1) 

@window.event 
def on_draw(): 
    window.clear() 
    sprite.draw() 

pyglet.app.run() 

我所得到的结果

enter image description here

回答

0

最简单的方法是使用精灵对象的.scale
它能够按比例缩放图像的原始尺寸,如果您自己调整图像大小,您无需担心映射数据或填充像素间隙。

为了帮助你去,这是一个实现的一个简单的例子:
(它看起来是这样的:https://youtu.be/Ly61VvTZnCU

import pyglet 
from pyglet.window import Platform 

monitor = Platform().get_default_display().get_default_screen() 

sprite = pyglet.sprite.Sprite(pyglet.resource.animation('anim.gif')) 

H_ratio = max(sprite.height, monitor.height)/min(sprite.height, monitor.height) 
W_ratio = max(sprite.width, monitor.width)/min(sprite.width, monitor.width) 

sprite.scale = min(H_ratio, W_ratio) # sprite.scale = 2 would double the size. 
            # We'll upscale to the lowest of width/height 
            # to not go out of bounds. Whichever 
            # value hits the screen edges first essentially. 

window = pyglet.window.Window(width=monitor.width, height=monitor.height, fullscreen=True) 

pyglet.gl.glClearColor(1, 1, 1, 1) 

@window.event 
def on_draw(): 
    window.clear() 
    sprite.draw() 

pyglet.app.run() 

我删除了一些您的演示/测试目的的代码。
该代码绝不是完美的,但它可能会让你了解这是如何工作的)。

相关问题