Skip to main content
 首页 » 编程设计

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

2025年05月04日7bonelee

我是 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

请您参考如下方法:

偶然发现了这首老歌,所以对于像我一样结束在这里的独行侠。更改 .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