- Pygame 教程
- Pygame - 主页
- Pygame - 概述
- Pygame - 你好世界
- Pygame - 显示模式
- Pygame - 本地模块
- Pygame - 颜色对象
- Pygame - 事件对象
- Pygame - 键盘事件
- Pygame - 鼠标事件
- Pygame - 绘制形状
- Pygame - 加载图像
- Pygame - 在窗口中显示文本
- Pygame - 移动图像
- Pygame - 使用数字键盘移动
- Pygame - 用鼠标移动
- Pygame - 移动矩形对象
- Pygame - 使用文本作为按钮
- Pygame - 转换图像
- Pygame - 声音对象
- Pygame - 混合器通道
- Pygame - 播放音乐
- Pygame - 玩电影
- Pygame - 使用相机模块
- Pygame - 加载光标
- Pygame - 访问 CDROM
- Pygame - 精灵模块
- Pygame - PyOpenGL
- Pygame - 错误和异常
- Pygame 有用资源
- Pygame - 快速指南
- Pygame - 有用的资源
- Pygame - 讨论
Pygame - 转换图像
pygame.ransform 模块包含许多函数的定义,用于操作从图像或文本块获取的 Surface 对象。表面的操作包括翻转、旋转、缩放、调整大小和缩放对象。
在 pygame.transform 模块中可以找到以下函数。
| 翻动() | 垂直和水平翻转 |
| 规模() | 调整大小到新分辨率 |
| 旋转() | 旋转图像 |
| 旋转缩放() | 过滤缩放和旋转 |
| 缩放2x() | 专门的图像倍增器 |
| 平滑缩放() | 平滑地将表面缩放到任意尺寸 |
| get_smoothscale_backend() | 返回正在使用的 smoothscale 滤波器版本 - 'GENERIC'、'MMX' 或 'SSE' |
| set_smoothscale_backend() | 将 smoothscale 滤波器版本设置为“GENERIC”、“MMX”或“SSE”之一 |
| 劈() | 获取已删除内部区域的图像的副本 |
| 拉普拉斯算子() | 查找曲面中的边缘 |
| 平均表面() | 从多个曲面中找出平均曲面。 |
| 平均颜色() | 求表面的平均颜色 |
| 临界点() | 查找表面中的哪些像素以及有多少像素在“search_color”或“search_surf”的阈值内。 |
让我们首先使用 Flip() 函数,其语法如下 -
flip(Surface, xbool, ybool)
此功能可以水平、垂直或两者都翻转表面对象。方向由两个布尔参数决定。
要水平翻转图像,请使用以下命令 -
pygame.transform.flip(img2,True, False)
要垂直翻转,请使用以下命令 -
pygame.transform.flip(img2,False, True)
在下面的示例中,pygame 徽标图像正常显示并双向翻转。首先从原始图像对象获取翻转表面,获取其 Rect 对象,然后构建它。要渲染水平翻转的图像,
img1 = pygame.image.load('pygame.png')
img2=img1
img2=pygame.transform.flip(img2,True, False)
#inside event loop
rect2 = img2.get_rect()
rect2.center = 200, 150
screen.blit(img2, rect2)
例子
渲染原始 Pygame 徽标及其翻转图像的完整代码如下 -
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption("Flip image")
img1 = pygame.image.load('pygame.png')
img2=img1
img3=img1
img2=pygame.transform.flip(img2,True, False)
img3=pygame.transform.flip(img3, False, True)
done = False
bg = (127,127,127)
while not done:
for event in pygame.event.get():
screen.fill(bg)
rect1 = img1.get_rect()
rect1.center = 200, 50
screen.blit(img1, rect1)
rect2 = img2.get_rect()
rect2.center = 200, 150
screen.blit(img2, rect2)
rect3 = img3.get_rect()
rect3.center = 200, 250
screen.blit(img3, rect3)
if event.type == pygame.QUIT:
done = True
pygame.display.update()
输出
rotate() 函数采用以下参数 -
rotate(Surface, angle)
例子
角度为负值时,表面沿顺时针方向旋转。
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption("rotate image")
img1 = pygame.image.load('pygame.png')
img2=img1
img3=img1
img2=pygame.transform.rotate(img2,90)
img3=pygame.transform.rotate(img3, -90)
done = False
bg = (127,127,127)
while not done:
for event in pygame.event.get():
screen.fill(bg)
rect1 = img1.get_rect()
rect1.center = 200, 50
screen.blit(img1, rect1)
rect2 = img2.get_rect()
rect2.center = 100, 200
screen.blit(img2, rect2)
rect3 = img3.get_rect()
rect3.center = 300,200
screen.blit(img3, rect3)
if event.type == pygame.QUIT:
done = True
pygame.display.update()
输出
例子
laplacian() 函数提取表面对象的轮廓。该函数只接受一个参数,即图像对象本身。
import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption("Laplacian of image")
img1 = pygame.image.load('pygame.png')
img2=img1
img2=pygame.transform.laplacian(img2)
done = False
bg = (127,127,127)
while not done:
for event in pygame.event.get():
screen.fill(bg)
rect1 = img1.get_rect()
rect1.center = 200, 50
screen.blit(img1, rect1)
rect2 = img2.get_rect()
rect2.center = 200, 200
screen.blit(img2, rect2)
if event.type == pygame.QUIT:
done = True
pygame.display.update()
输出
要使 Surface 对象随着鼠标移动而移动,请计算距图像中心的 x、y 坐标。我们还计算了鼠标中心距离 d。atan2(y, x) 数学函数可以找到旋转角度。我们需要将弧度转换为度数。根据鼠标中心的距离,我们计算比例参数。
mouse = event.pos Pygame 54 x = mouse[0] - 200 y = mouse[1] - 150 d = math.sqrt(x ** 2 + y ** 2) angle = math.degrees(-math.atan2(y, x)) scale = abs(5 * d / 400)
最后,我们使用 rotzoom() 函数执行组合旋转和缩放变换。
rotozoom(Surface, angle, scale)
例子
以下代码渲染了可以根据鼠标移动旋转的 Pygame 徽标图像。
import pygame , math
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption("Move image with mouse")
img1 = pygame.image.load('pygame.png')
done = False
bg = (127,127,127)
while not done:
for event in pygame.event.get():
screen.fill(bg)
if event.type == pygame.QUIT:
done = True
if event.type == MOUSEMOTION:
mouse = event.pos
x = mouse[0] - 200
y = mouse[1] - 150
d = math.sqrt(x ** 2 + y ** 2)
angle = math.degrees(-math.atan2(y, x))
scale = abs(5 * d / 400)
img2 = pygame.transform.rotozoom(img1, angle, scale)
rect = img2.get_rect()
rect.center = (200,150)
screen.blit(img2, rect)
pygame.display.update()
输出
运行上面的代码,尝试沿着显示窗口移动鼠标光标。图像应旋转并相应地缩小或放大。
