如何在Python中使用Pyglet调整图像大小

5 投票
1 回答
9584 浏览
提问于 2025-04-18 13:34

我刚开始接触Pyglet(还有stackoverflow),现在不知道怎么调整图片的大小。

我想调整的图片是'pipe.png'。

用我现在的代码,图片没有完全显示,因为窗口太小了。

我想把图片的大小调整一下,让它能适应窗口。

现在'pipe.png'的大小是100x576。

import pyglet

window = pyglet.window.Window()

pyglet.resource.path = ["C:\\"]
pipe = pyglet.resource.image('pipe.png') 
pyglet.resource.reindex()  

@window.event                    
def on_draw():
    window.clear()
    pipe.blit(0, 0)

pyglet.app.run()

补充:

我最后在这里找到了答案:

http://pyglet.org/doc-current/programming_guide/image.html#simple-image-blitting

解决方案是:

imageWidth = 100
imageHeight = 100

imageName.width = imageWidth
imageName.height = imageHeight

这样可以把图片的显示大小调整为100x100。

1 个回答

5

我遇到了这个老问题,所以如果你和我一样孤单地来到这里,听我说说。单纯改变 .width.height 可能在很多情况下没什么用(现在可能根本没用?)。

要想成功改变图片的分辨率,你需要修改它的 .scale 属性。

下面是我用来调整图片大小的一段代码:

from pyglet.gl import *

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)

image = pyglet.image.load('test.png')
height, width = 800, 600 # Desired resolution

# the min() and max() mumbo jumbo is to honor the smallest requested resolution.
# this is because the smallest resolution given is the limit of say
# the window-size that the image will fit in, there for we can't honor
# the largest resolution or else the image will pop outside of the region.
image.scale = min(image.height, height)/max(image.height, height)), max(min(width, image.width)/max(width, image.width)

# Usually not needed, and should not be tampered with,
# but for a various bugs when using sprite-inheritance on a user-defined
# class, these values will need to be updated manually:
image.width = width
image.height = height
image.texture.width = width
image.texture.height = height

撰写回答