How to change an image size in Pygame?
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:
- Creates a new surface (
newSurface
) with size(width, height)
. - Scale and copy
Surface
tonewSurface
. - 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()
Changing size of image in Pygame
You need to assign pygame.transform.scale(img,(35,35))
to a variable otherwise it won't be saved.
Also, the error message is a bug in an old version of pygame, try updating pygame by using python3 -m pip install -U pygame
or if that doesn't work, try python -m pip install Pygame>=2.0.0.dev6
.
How to change an image size in Pygame?
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:
- Creates a new surface (
newSurface
) with size(width, height)
. - Scale and copy
Surface
tonewSurface
. - 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()
How to resize image with pygame.transform.scale?
For a "step on" effect you have to change the content of the map. Use a new letter (e.g. "S") for the grass stepped on (stepped_on_grass.png
):
map_tile_images = {
"G": pygame.transform.scale(pygame.image.load('imgs/grass.png'), (SCALE, SCALE)),
"W": pygame.transform.scale(pygame.image.load('imgs/water.png'), (SCALE, SCALE)),
"0": pygame.transform.scale(pygame.image.load('imgs/none.png'), (SCALE, SCALE)),
"T": pygame.transform.scale(pygame.image.load('imgs/tree1.png'), (SCALE, SCALE)),
"S": pygame.transform.scale(pygame.image.load('imgs/stepped_on_grass.png'), (SCALE, SCALE)),
}
Pass the map
to the Player.update_position
method:
class Game:
# [...]
def move_unit(self, unit, position_change):
# [...]
unit.update_position(new_position, self.map)
Change the content of the map in Player.update_position
:
class Player:
# [...]
def update_position(self, new_position, map):
game.map[self.position [1]][self.position [0]] = 'S'
self.position = new_position
Related Topics
Why Does Python Assignment Not Return a Value
Remove Xticks in a Matplotlib Plot
How to Customise Qgroupbox Title in Pyqt5
Comparison of R, Statmodels, Sklearn for a Classification Task with Logistic Regression
How to Import a JSON from a File on Cloud Storage to Bigquery
How to Link Pycharm with Pyspark
MAC Osx Python Ssl.Sslerror: [Ssl: Certificate_Verify_Failed] Certificate Verify Failed (_Ssl.C:749)
Getting an Instance Name Inside Class _Init_()
Efficient Calculation of Fibonacci Series
Python Parse CSV Ignoring Comma with Double-Quotes
Asynchronous Method Call in Python
Using Beautiful Soup to Convert CSS Attributes to Individual HTML Attributes
How to Get Access of Individual Trees of a Xgboost Model in Python /R
What Does Blazeds Livecycle Data Services Do, That Something Like Pyamf or Rubyamf Not Do
How to Convert a Dictionary into a List of Tuples
How to Change the Default MySQL Connection Timeout When Connecting Through Python
Popen Waiting for Child Process Even When the Immediate Child Has Terminated