How to Rotate and Scale images using PyGame ?
Last Updated :
14 Feb, 2023
In this article, we are going to see how to Rotate and Scale the image. Image Scaling refers to the resizing of the original image and Image Rotation refers to turning off an image with some angle. Rotations in the coordinate plane are counterclockwise. Let’s proceed with the methods used and the complete code to perform these tasks.
Scaling the Image
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.
Example:
Image Used:
Python3
import pygame
pygame.init()
size = width,height = 600 , 600
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
image = pygame.image.load( 'gfg.png' )
DEFAULT_IMAGE_SIZE = ( 200 , 200 )
image = pygame.transform.scale(image, DEFAULT_IMAGE_SIZE)
DEFAULT_IMAGE_POSITION = ( 200 , 200 )
running = False
while not running:
for event in pygame.event.get():
if event. type = = pygame.QUIT:
running = True
screen.fill(( 0 , 0 , 0 ))
screen.blit(image, DEFAULT_IMAGE_POSITION)
pygame.display.flip()
clock.tick( 30 )
|
Output:
Rotating the Image
To rotate the image we use the pygame.transform.rotate(image, degree) method where we pass the image that we are going to rotate and the degree by which rotation is to be done.
Example:
Python3
import pygame
pygame.init()
size = width,height = 600 , 600
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
image = pygame.image.load( 'gfg.png' )
DEFAULT_IMAGE_SIZE = ( 200 , 200 )
image = pygame.transform.rotate(image, 180 )
DEFAULT_IMAGE_POSITION = ( 200 , 200 )
running = False
while not running:
for event in pygame.event.get():
if event. type = = pygame.QUIT:
running = True
screen.fill(( 0 , 0 , 0 ))
screen.blit(image, DEFAULT_IMAGE_POSITION)
pygame.display.flip()
clock.tick( 30 )
|
Output:
Rotating and Scaling the Image
Let us see how to perform the Scaling and Rotation of an image given. We will set the default image size that is agreeable and the default image position where we want to see our image on the window screen. The same methods that are explained above will be used for scaling and rotation the image.
Example:
Python3
import pygame
pygame.init()
size = width,height = 600 , 600
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
image = pygame.image.load( 'gfg.png' )
DEFAULT_IMAGE_SIZE = ( 200 , 200 )
image = pygame.transform.scale(image, DEFAULT_IMAGE_SIZE)
image = pygame.transform.rotate(image, 90 )
DEFAULT_IMAGE_POSITION = ( 200 , 200 )
running = False
while not running:
for event in pygame.event.get():
if event. type = = pygame.QUIT:
running = True
screen.fill(( 0 , 0 , 0 ))
screen.blit(image, DEFAULT_IMAGE_POSITION)
pygame.display.flip()
clock.tick( 30 )
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...