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