Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Pygame
3.
Scaling the Image
3.1.
INPUT
4.
Rotating the Image
5.
Rotating and Scaling the Image
5.1.
Example:
6.
Frequently Asked Questions.
6.1.
Can you rotate an image in pygame?
6.2.
How do you scale a surface in pygame?
6.3.
How do you scale an image in pygame?
6.4.
What is pygame display Flip ()?
6.5.
What is the Pygame surface?
7.
Conclusion
Last Updated: Mar 27, 2024

Rotating and Scaling the image in pygame

Introduction

Pygame is a Python-based multimedia toolkit for creating games and other multimedia applications. In this course, the pygame module will be used to create various forms on the screen while considering their height, width, and position inside the pygame window.

Pygame is a Python wrapper for SDL, which stands for Simple DirectMedia Layer. SDL allows you to access your system's multimedia hardware from any platform, including sound, video, mouse, keyboard, and joystick. To replace the now-defunct PySDL project, the pygame project was formed. Since they are cross-platform, you can develop games and rich multimedia Python programs on any device that supports SDL and pygame since they are cross-platform!

Pygame

We'll learn how to Rotate and Scale a picture in this post. Picture Scaling is the process of scaling an image, whereas Image Rotation is rotating an appearance at an angle. In the coordinate plane, rotations are counterclockwise. Let's look at the methods utilized and the whole code used to achieve these tasks.

Scaling the Image

We use the pygame.transform.scale(image, DEFAULT IMAGE SIZE) function to scale the picture, passing the image to be mounted and the default image size we will modify manually according to our needs.

Also Read - Image Sampling

INPUT

# Import pygame
import pygame
# Initialise pygame
pygame.init()
# Set window size
sze = width,height = 600, 600
scrn = pygame.display.set_mode(sze)
# Clock
clck = pygame.time.Clock()
# Load image
img = pygame.image.load('cn_pygame.png')
# Set the size for the image
DFLT_IMG_SZ = (200, 200)
# Scale the image to your needed size
img = pygame.transform.scale(img, DFLT_IMG_SZ)
# Set a default position
DFLT_IMG_PSTN = (200,200)
# Prepare loop condition
rnnng = False
# Event loop
while not rnnng:
# Close window event
for vnt in pygame.event.get():
if vnt.type == pygame.QUIT:
rnnng = True
# Background Color
scrn.fill((0, 0, 0))
# Show the image
scrn.blit(img, DFLT_IMG_PSTN)
# Part of event loop
pygame.display.flip()
clck.tick(30)
You can also try this code with Online Python Compiler
Run Code

OUTPUT

Rotating the Image

To rotate an image, we use the pygame.transform.rotate(image, degree) function, passing the image to be rotated and the degree by which it should be turned.

Example:

# Import pygame
import pygame
# Initialise pygame
pygame.init()
# Set window size
size = width,height = 600, 600
screen = pygame.display.set_mode(size)
# Clock
clock = pygame.time.Clock()
# Load image
image = pygame.image.load('cn_pygame.png')
# Set the size for the image
DEFAULT_IMAGE_SIZE = (200, 200)
# Rotate the image by any degree
image = pygame.transform.rotate(image, 180)
# Set a default position
DEFAULT_IMAGE_POSITION = (200,200)
# Prepare loop condition
running = False
# Event loop
while not running:
# Close window event
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = True
# Background Color
screen.fill((0, 0, 0))
# Show the image
screen.blit(image, DEFAULT_IMAGE_POSITION)
# Part of event loop
pygame.display.flip()
clock.tick(30)
You can also try this code with Online Python Compiler
Run Code

OUTPUT

Rotating and Scaling the Image

Let's look at how to conduct Scaling and Rotation on a given image. We'll choose an acceptable default image size and a default picture position on the window screen where we want to see our image. Scaling and rotation of the picture will be done using the same procedures.

Example:

# Import pygame
import pygame
# Initialise pygame
pygame.init()
# Set window size
sz = width,height = 600, 600
scrn = pygame.display.set_mode(sz)
# Clock
clck = pygame.time.Clock()
# Load image
img = pygame.image.load('cn_pygame.png')
# Set the size for the image
DFLT_IMG_SZ = (200, 200)
# Scale the image to your needed size
img = pygame.transform.scale(image, DFLT_IMG_SZ)
# Rotate the image by any degree
img = pygame.transform.rotate(img, 90)
# Set a default position
DFLT_IMG_POSITION = (200,200)
# Prepare loop condition
running = False
# Event loop
while not running:
# Close window event
for vnt in pygame.event.get():
if vnt.type == pygame.QUIT:
running = True
# Background Color
screen.fill((0, 0, 0))
# Show the image
screen.blit(image, DFLT_IMG_POSITION)
# Part of event loop
pygame.display.flip()
clock.tick(30)
You can also try this code with Online Python Compiler
Run Code

OUTPUT:

Frequently Asked Questions.

Can you rotate an image in pygame?

pygame. transform. rotate may rotate an image (pygame. Surface).

How do you scale a surface in pygame?

pygame. transform. scale is the resizing method you're searching for. It will return a new surface or insert the result into an already generated cover if you pass it a character and the new size you desire (this is faster).

How do you scale an image in pygame?

We use the pygame. transform. scale(image, DEFAULT IMAGE SIZE) function to scale the picture, passing the image to be mounted and the default image size to be changed manually according to our needs.

What is pygame display Flip ()?

flip() is a function for software displays. It just permits a section of the screen to be refreshed, rather than the complete screen. If no argument is given, it restores the whole Surface area in the same way as pygame does.

What is the Pygame surface?

A surface may represent a game written in Python or Any image. The Surface has a set pixel format and resolution. Color palettes are used to translate 8-bit pixels to 24-bit color on surfaces. To use pygame, type pygame on the command prompt. To build a new image object, use the Surface() pygame object for expressing pictures.

Conclusion

So that's the end of the article. Rotating and Scaling the image in pygame

After reading about Rotating and Scaling the image in pygame, Are you interested in reading/exploring more articles on the subject of pygame? Don't worry; Coding Ninjas has you covered.

However, if you want to give your work an edge over the competition, you might choose to enroll in one of our premium courses.

With our Coding Ninjas Studio Guided Path, you may learn about Data Structures & Algorithms, Competitive Programming, JavaScript, System Design, and more! If you want to put your coding skills to the test, check out the mock test series on Coding Ninjas Studio and participate in the contests! But if you've only recently started your schooling and are looking for answers to issues presented by digital titans like Amazon, Microsoft, Uber, and others. In this situation, you must consider the obstaclesinterview experiences, and interview package as part of your placement preparations. If you find our blogs valuable and fascinating, please vote them up!

Good luck with your studies!

 

Live masterclass