Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change an image size in Pygame?

Tags:

pygame

There is the image I'm importing :

look_1 = pygame.image.load('data\\png\\look1.png').convert_alpha()

And what I tried to get its size reduce was this :

pygame.transform.scale()

But this seems not to be the right way to do it.

like image 680
Val_ Avatar asked Mar 27 '17 12:03

Val_


People also ask

How do you change the size of a sprite in pygame?

You can make a Sprite bigger or smaller with the SET SIZE command. ACTIONS Toolkit - Scroll down to SPRITE SETTINGS - Set Size Block. Use a number less than 1 to make your sprite smaller and a number bigger than 1 to make the sprite larger.

How do I make an image smaller in Python?

To resize an image, you call the resize() method on it, passing in a two-integer tuple argument representing the width and height of the resized image. The function doesn't modify the used image; it instead returns another Image with the new dimensions.

How do I resize an image in Python pygame?

To scale the image we use the pygame. transform. scale(image, DEFAULT_IMAGE_SIZE) method where we pass the image that we are going to scale and the default image size that we will set manually according to our need.


2 Answers

You can either use pygame.transform.scale or smoothscale and pass the new width and height of the surface or pygame.transform.rotozoom and pass a float that will be multiplied by the current resolution.

import sys
import pygame as pg

pg.init()
screen = pg.display.set_mode((640, 480))

IMAGE = pg.Surface((100, 60))
IMAGE.fill(pg.Color('sienna2'))
pg.draw.circle(IMAGE, pg.Color('royalblue2'), (50, 30), 20)
# New width and height will be (50, 30).
IMAGE_SMALL = pg.transform.scale(IMAGE, (50, 30))
# Rotate by 0 degrees, multiply size by 2.
IMAGE_BIG = pg.transform.rotozoom(IMAGE, 0, 2)


def main():
    clock = pg.time.Clock()
    done = False
    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True

        screen.fill(pg.Color('gray15'))
        screen.blit(IMAGE, (50, 50))
        screen.blit(IMAGE_SMALL, (50, 155))
        screen.blit(IMAGE_BIG, (50, 230))
        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    main()
    pg.quit()
    sys.exit()
like image 68
skrx Avatar answered Oct 07 '22 06:10

skrx


You have to decide if you want to use Use pygame.transform.smoothscale or pygame.transform.scale. While pygame.transform.scale performs a fast scaling with the nearest pixel, pygame.transform.smoothscale scales a surface smoothly to any size with interpolation of the pixels.
Scaling up a Surface with pygame.transform.scale() will result in a jagged result. When downscaling you lose information (pixels). In comparison, pygame.transform.smoothscale blurs the Surface.

pygame.transform.scale() and pygame.transform.smoothscale are used in the same way. They do not scale the input Surface itself. It creates a new surface and does a scaled "blit" to the new surface. The new surface is returned by the return value. They:

  1. Creates a new surface (newSurface) with size (width, height).
  2. Scale and copy Surface to newSurface.
  3. Return newSurface.
look_1 = pygame.image.load('data\\png\\look1.png').convert_alpha()
look_1 = pygame.transform.scale(look_1, (new_width, new_height)) 

or

look_1 = pygame.image.load('data\\png\\look1.png').convert_alpha()
look_1 = pygame.transform.smoothscale(look_1, (new_width, new_height)) 

See also Transform scale and zoom surface

Minimal example: replit.com/@Rabbid76/PyGame-ScaleCenter

import pygame

class ScaleSprite(pygame.sprite.Sprite):
    def __init__(self, center, image):
        super().__init__()
        self.original_image = image
        self.image = image
        self.rect = self.image.get_rect(center = center)
        self.mode = 1
        self.grow = 0

    def update(self):
        if self.grow > 100:
            self.mode = -1
        if self.grow < 1:
            self.mode = 1
        self.grow += 1 * self.mode 

        orig_x, orig_y = self.original_image.get_size()
        size_x = orig_x + round(self.grow)
        size_y = orig_y + round(self.grow)
        self.image = pygame.transform.scale(self.original_image, (size_x, size_y))
        self.rect = self.image.get_rect(center = self.rect.center)

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

sprite = ScaleSprite(window.get_rect().center, pygame.image.load("Banana64.png"))
group = pygame.sprite.Group(sprite)

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    group.update()

    window.fill(0)
    group.draw(window)
    pygame.display.flip()

pygame.quit()
exit()
like image 30
Rabbid76 Avatar answered Oct 07 '22 05:10

Rabbid76