Article directory
function list
Pygame's transform encapsulates some basic image processing functions, the list is as follows
function | Function |
---|---|
flip | Mirror |
scale | Scale to new resolution |
scale_by | Scale by factor |
scale2x | Professional image multiplier |
rotate | rotate |
rotozoom | Zoom and rotate |
smoothscale | Smooth zoom |
smoothscale_by | Smooth scaling to new resolutions |
chop | Get a copy of the image with the inner areas removed |
laplacian | edge search |
average_surfaces | Averaging multiple images |
average_color | Image color mean |
grayscale | Grayscale |
threshold | The number of pixels within a certain threshold |
Image display
In order to demonstrate the effects of these functions, first make a function to display images through pygame
import pygame
def showImg(img):
pygame.init()
rect = img.get_rect()
screen = pygame.display.set_mode((rect.width, rect.height))
while True:
for e in pygame.event.get():
if e.type==pygame.QUIT:
pygame.quit()
return
screen.blit(img, rect)
pygame.display.flip()
ball = pygame.image.load("intro_ball.gif")
showImg(ball)
flip
Flip is used for flipping. In addition to the input image, there are two Boolean parameters, which are used to control the horizontal and vertical directions respectively. True means flipping, and False means not flipping.
import pygame.transform as pt
xFlip = pt.flip(ball, True, False)
yFlip = pt.flip(ball, False, True)
xyFlip = pt.flip(ball, True, True)
The effects are as follows
Zoom
Compared with mirroring, zooming is obviously a more common operation in games. In the transform module, two scaling solutions are provided
- scale(surface, size, dest_surface=None)
- scale_by(surface, factor, dest_surface=None)
Among them, scale scales the original image to the given size; scale_by scales the image according to the given factor. If factor is a number, then the same scaling will be performed in both directions. If A tuple, such as ( 3 , 4 ) (3,4) (3,4), then scale the two dimensions by 3 times and 4 times respectively. Examples are as follows
xScale = pt.scale_by(ball, (2,1))
yScale = pt.scale(ball, (111,222))
In addition, smoothscale and smoothscale_by perform bilateral filtering and smoothing on the basis of scaling, making the scaling look more natural.
rotate
The image can be rotated through rotate. There are two input parameters, namely the image to be rotated and the rotation angle. This function can be seamlessly inserted into the previous flat tossing motion, that is, the ball will have a spin when it is flat tossed. The example is as follows
code show as below
import time
pygame.init()
size = width, height = 640, 320
speed = [10, 0]
screen = pygame.display.set_mode(size)
ball = pygame.image.load("intro_ball.gif")
rect = ball.get_rect()
th = 0
while True:
if pygame.QUIT in [e.type for e in pygame.event.get()]:
break
time.sleep(0.02)
rect = rect.move(speed)
if rect.right>width:
speed = [10, 0]
rect = ball.get_rect()
if rect.bottom>height:
speed[1] = -speed[1]
speed[1] += 1
th += 5
screen.fill("black")
screen.blit(pt.rotate(ball, th), rect)
pygame.display.flip()
pygame.quit()