【问题标题】:How Do You Resize an Image in Python Using Pyglet如何使用 Pyglet 在 Python 中调整图像大小
【发布时间】:2014-07-16 18:23:48
【问题描述】:

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

【问题讨论】:

  • 如果你保持纵横比,那就太棒了;)
  • 我认为您的编辑不是正确的解决方案。根据我的测试,您的代码最终将只绘制图像的底部 100x100。此外,您的链接似乎没有指向正确的页面。

标签: python image pyglet


【解决方案1】:

遇到了这个老家伙,所以对于像我一样最终来到这里的独行侠。在很多情况下(或者这些天?),更改 .width.height 并不会起到太大作用。

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

这是我用来调整图像大小的 sn-p 代码:

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

【讨论】:

  • 有没有办法以这种方式重复图像的内部?澄清一下:我有一个平台的“图像”(它只是两个不同高度的空心矩形),我希望能够更改图像的宽度而不会出现缩放/拉伸问题。有什么方法可以做到这一点(即使使用原始图像数据)还是我必须使用 pyglet 用 openGL 绘制矩形并忘记图像?
  • @BeckettO'Brien 我会画一个矩形(并设置宽度)。缩放图像几乎总是意味着数据的丢失,以及之后的质量。所以绘制基元(矩形、三角形和线)是要走的路。将它们分批存储,否则它们会变得非常 GPU/CPU 密集型。
  • 看起来仅仅设置宽度和高度就可以拉伸图像以适应
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 2010-11-27
  • 1970-01-01
  • 2016-10-04
  • 2015-06-04
  • 2021-04-09
相关资源
最近更新 更多