1. ホーム
  2. スクリプト・コラム
  3. パイソン

Pygame Transformによる画像モーフィングの実装例

2022-01-27 07:58:18

pygame.transformモジュールでは、以下の一般的なメソッドに示すように、ロードして作成した画像に対して、画像のサイズ変更、回転など、さまざまな操作を行うことができます。

ここでは、簡単なデモを紹介します。

import pygame
# Introduce all constants in pygame, such as QUIT
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500,250))
pygame.display.set_caption('c language Chinese')
# load an image (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 image
# print(type(image_new))
# Rotate the newly generated image to 45 degrees
image_1 = pygame.transform.rotate(image_new,45)
# Rotate by 0 degrees using rotozoom() to shrink the image by 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()
    # Add the last generated image_2 to the display screen
    screen.blit(image_2,(0,0))
    pygame.display.update()


実装例

import pygame

pygame.init()
screen = pygame.display.set_mode((960, 600))
pygame.display.set_caption("image_transform")
img = pygame.image.load('horse.jpg')
clock = pygame.time.Clock()

img1 = pygame.transform.flip(img,False, True) # image to be flipped horizontally and vertically
#parameter1: the image to be flipped
#parameter2: whether to flip horizontally or not
#Parameter 3: whether to flip vertically or not
#return a new image

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)) #scale
#parameter2: width and height of the new image

img1 = pygame.transform.smoothscale(img,(400,300)) #smoothscale image
#This function only works with 24-bit or 32-bit surfaces. If the input surface bit depth is less than 24, an exception is thrown


img1 = pygame.transform.scale2x(img) # fast double size zoom

img = pygame.image.load('horse.jpg')
img1 = pygame.transform.rotate(img, 30) # rotate the image
#parameter 2: angle to rotate - positive number means counterclockwise - negative number means clockwise
# Unless rotated in 90 degree increments, the image will be padded to a larger size. If the image has pixel alpha, the filled area will be transparent
#rotation is around the center

img1 = pygame.transform.rotozoom(img, 30.0, 2.0) #scale+rotate
#The first parameter specifies the image to be processed, the second parameter specifies the number of angles to rotate, and the third parameter specifies the scale of the zoom
#This function will filter the image, the image will be better, but will be much slower

img1 = pygame.transform.chop(img, (0, 0, 100, 50)) #Crop the image
#The first parameter specifies the image to crop, and the second parameter specifies the area of the image to keep

img = pygame.image.load('horse.jpg')
img1 = pygame.transform.laplacian(img) # find the edge - outline

上記はPygame Transformの画像変換の実装例の詳細ですが、Pygame Transformの画像変換については、スクリプトハウスの他の関連記事にも注目してください!