Implementation Example of Pygame Transform Image Deformation
- 2021-12-13 08:24:44
- OfStack
The pygame. transform module allows you to perform a series of operations on loaded and created images, such as resizing images, rotating images, etc. The common methods are as follows:
Let's look at a set of simple demonstration examples:
import pygame
# Introduce pygame All constants in, such as QUIT
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500,250))
pygame.display.set_caption('c Language Chinese Network ')
# Loading 1 Pictures ( 455*191)
image_surface = pygame.image.load("C:/Users/Administrator/Desktop/c-net.png").convert()
image_new = pygame.transform.scale(image_surface,(300,300))
# View the object type of the newly generated picture
#print(type(image_new))
# Rotate the newly generated image to 45 Degrees
image_1 =pygame.transform.rotate(image_new,45)
# Use rotozoom() Rotate 0 Degree, reduce the image 0.5 Times
image_2 = pygame.transform.rotozoom(image_1,0,0.5)
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
# The last generated image_2 Add to the display screen
screen.blit(image_2,(0,0))
pygame.display.update()
Implementation example
import pygame
pygame.init()
screen = pygame.display.set_mode((960, 600))
pygame.display.set_caption(" Image transformation ")
img = pygame.image.load(' Horse .jpg')
clock = pygame.time.Clock()
img1=pygame.transform.flip(img,False, True) # The image is flipped horizontally and vertically
# Parameter 1 Image to flip
# Parameter 2 Whether horizontal is flipped or not
# Parameter 3 Whether vertical is flipped or not
# Return 1 New images
while True:
t = clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
screen.blit(img1,(100,50))
pygame.display.update()
img1 = pygame.transform.scale(img, (200, 100)) # Zoom
# Parameter 2 Width and height of new image
img1 = pygame.transform.smoothscale(img,(400,300)) # Smooth scaling image
# This function only applies to 24 Bit or 32 Bit surface . If the input surface bit depth is less than 24 An exception is thrown
img1 = pygame.transform.scale2x(img) # Fast double-size magnification
img = pygame.image.load(' Horse .jpg')
img1 = pygame.transform.rotate(img, 30) # Rotating image
# Parameter 2 Angle to rotate -- Positive numbers denote counterclockwise -- Negative numbers indicate clockwise
# Unless with 90 Otherwise, the image will be filled to a larger size. If the image has pixels alpha The padding area will be transparent
# Rotation is around the center
img1 = pygame.transform.rotozoom(img, 30.0, 2.0) # Zoom + Rotate
# No. 1 1 Specifies the image to be processed, the 2 The number of parameters specifies the number of angles to rotate, the 3 Parameters specify the scale
# This function will filter the image, and the image will be better, but the speed will be much slower
img1 = pygame.transform.chop(img, (0, 0, 100, 50)) # Cut the image
# No. 1 1 Parameter specifies the image to be trimmed, and the 2 Parameters specify the area of the image to preserve
img = pygame.image.load(' Horse .jpg')
img1 = pygame.transform.laplacian(img) # Find edge -- Outline
The above is the Pygame Transform image deformation implementation example details, more about Pygame Transform image deformation information please pay attention to other related articles on this site!