【问题标题】:How do I make gif animation match fullscreen window size in Python using Pyglet?如何使用 Pyglet 在 Python 中使 gif 动画匹配全屏窗口大小?
【发布时间】:2017-10-05 12:18:40
【问题描述】:

所以,几个小时前,我发现 Pyglet 最适合我渲染 gif 动画,所以我是新的。我的问题是动画 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()

我得到的结果

【问题讨论】:

    标签: python python-2.7 pyglet


    【解决方案1】:

    最简单的方法是使用精灵对象的.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()
    

    出于演示/测试目的,我删除了您的一些代码。
    代码绝不是完美的,但希望它能让您深入了解它是如何工作的)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-07
      • 2022-06-18
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多