How Do You Resize an Image in Python Using Pyglet

image, pyglet, python

Solution

Came across of this oldie, so for whoever lone ranger that ends up here juast as I did. Changing `.width` and `.height` won't do much in many cases (or at all these days?).

In order to successfully change a image resolution, you need to modify it's `.scale` attribute.

Here's a snippet of code that I use to resize a image:

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

Problem

I'm new to Pyglet (and stackoverflow) and cannot seem to find out how to resize images. 'pipe.png' is the image that I am trying to adjust the size of. With this code, the image is not fully shown because the window size is too small. I would like to adjust the size of the image so that it fits inside of the window. The current size of 'pipe.png' is 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() ``` EDIT: I ended up finding out the answer here: http://pyglet.org/doc-current/programming_guide/image.html#simple-image-blitting The solution is: ``` imageWidth = 100 imageHeight = 100 imageName.width = imageWidth imageName.height = imageHeight ``` This would adjust to image size to display as 100x100

Original source