pygame.transform
pygame module to transform surfaces
— flip vertically and horizontally
— resize to new resolution
— resize to new resolution, using scalar(s)
— rotate an image
— filtered scale and rotation
— specialized image doubler
— scale a surface to an arbitrary size smoothly
— resize to new resolution, using scalar(s)
— return smoothscale filter version in use: 'GENERIC', 'MMX', or 'SSE'
— set smoothscale filter version to one of: 'GENERIC', 'MMX', or 'SSE'
— gets a copy of an image with an interior area removed
— find edges in a surface
— find the average surface from many surfaces.
— finds the average color of a surface
— finds which, and how many pixels in a surface are within a threshold of a 'search_color' or a 'search_surf'.

曲面变换是移动像素或调整像素大小的操作。所有这些函数都需要一个Surface对其进行操作,并返回一个新的Surface和结果。

其中一些变化被认为是破坏性的。这意味着每次执行这些操作时,都会丢失像素数据。常见的例子是调整大小和旋转。因此,最好重新变换原始曲面,而不是多次变换图像。(例如,假设您正在设置一个弹跳弹簧的动画,该弹簧会膨胀和收缩。如果将大小更改以增量方式应用于以前的图像,则会丢失细节。相反,始终从原始图像开始并缩放到所需的大小。)

Changed in pygame 2.0.2: 转换函数现在支持关键字参数。

pygame.transform.flip()
flip vertically and horizontally
flip(surface, flip_x, flip_y) -> Surface

这可以垂直、水平或同时垂直和/或翻转曲面。这些论据 flip_x 和 flip_y 是控制是否翻转每个轴的布尔值。翻转曲面是非破坏性的,并返回具有相同尺寸的新曲面。

pygame.transform.scale()
resize to new resolution
scale(surface, size, dest_surface=None) -> Surface

将曲面大小调整为新的大小,指定为(宽度、高度)。这是一种不对结果进行采样的快速缩放操作。

可以使用可选的目标曲面,而不是让它创建新的目标曲面。如果您想要重复缩放某些内容,此操作会更快。但是,目标的大小必须与传入的大小(宽度、高度)相同。此外,目标曲面必须具有相同的格式。

pygame.transform.scale_by()
resize to new resolution, using scalar(s)
scale_by(surface, factor, dest_surface=None) -> Surface

相同于 scale() ,但按某个因子进行缩放,而不是显式地采用新的大小。例如, transform.scale_by(surf, 3) 将使曲面在两个维度上的大小增加三倍。或者,比例因子可以是两个数字的序列,分别控制x和y的缩放。例如, transform.scale_by(surf, (2, 1)) 使图像宽度加倍,但保持高度不变。

New in pygame 2.1.3.

pygame.transform.rotate()
rotate an image
rotate(surface, angle) -> Surface

未过滤的逆时针旋转。角度参数表示度,可以是任何浮点值。负角度值将顺时针旋转。

除非以90度为增量旋转,否则图像将被填充得更大以保持新大小。如果图像包含像素Alpas,则填充区域将是透明的。否则,PYGAME将选取与Surface Colorkey或Topleft像素值匹配的颜色。

pygame.transform.rotozoom()
filtered scale and rotation
rotozoom(surface, angle, scale) -> Surface

这是一个组合的缩放和旋转变换。生成的Surface将是经过过滤的32位Surface。Scale参数是一个将乘以当前分辨率的浮点值。角度参数是一个浮点值,表示要旋转的逆时针角度。负旋转角度将顺时针旋转。

pygame.transform.scale2x()
specialized image doubler
scale2x(surface, dest_surface=None) -> Surface

这将返回一个两倍于原始图像大小的新图像。它使用的是AdvanceMAME Scale2X算法,该算法可以实现无锯齿的位图图形比例。

这真的只对纯色的简单图像有影响。在摄影和抗锯齿图像上,它看起来像是一个常规的未过滤比例。

可以使用可选的目标曲面,而不是让它创建新的目标曲面。如果您想要重复缩放某些内容,此操作会更快。但是,目标必须是传入的源图面大小的两倍。此外,目标曲面必须具有相同的格式。

pygame.transform.smoothscale()
scale a surface to an arbitrary size smoothly
smoothscale(surface, size, dest_surface=None) -> Surface

根据需要使用两种不同的算法之一来缩放输入曲面的每个维度。对于收缩,输出像素是它们覆盖的颜色的面积平均值。对于扩展,使用双线性滤波器。针对x86-64和i686架构进行了优化 MMX 包括例程,并且运行速度将比其他类型的机器快得多。大小是(宽度、高度)的2个数字序列。此函数仅适用于24位或32位曲面。如果输入表面位深度小于24,则会引发异常。

New in pygame 1.8.

pygame.transform.smoothscale_by()
resize to new resolution, using scalar(s)
smoothscale_by(surface, factor, dest_surface=None) -> Surface

相同于 smoothscale() ,但按某个因子进行缩放,而不是显式地采用新的大小。例如, transform.smoothscale_by(surf, 3) 将使曲面在两个维度上的大小增加三倍。或者,比例因子可以是两个数字的序列,分别控制x和y的缩放。例如, transform.smoothscale_by(surf, (2, 1)) 使图像宽度加倍,但保持高度不变。

New in pygame 2.1.3.

pygame.transform.get_smoothscale_backend()
return smoothscale filter version in use: 'GENERIC', 'MMX', or 'SSE'
get_smoothscale_backend() -> string

显示是否正在使用平滑缩放 MMX 或 SSE 加速。如果没有可用的加速,则返回“Generic”。对于x86处理器,要使用的加速级别在运行时确定。

提供此功能是为了进行电子游戏测试和调试。

pygame.transform.set_smoothscale_backend()
set smoothscale filter version to one of: 'GENERIC', 'MMX', or 'SSE'
set_smoothscale_backend(backend) -> None

设置平滑比例加速。接受字符串参数。如果值为“Generic”,则会关闭加速。“MMX”使用 MMX 仅限使用说明。“SSE”允许 SSE 扩展也是如此。如果当前处理器不识别或不支持类型,则会引发值错误。

提供此功能是为了进行电子游戏测试和调试。如果平滑比例导致无效指令错误,则应报告的是pyGame/SDL错误。仅将此函数用作临时修复程序。

pygame.transform.chop()
gets a copy of an image with an interior area removed
chop(surface, rect) -> Surface

提取图像的一部分。将删除给定矩形区域周围的所有垂直和水平像素。然后将拐角区域(与矩形对角线)放在一起。(此操作不会更改原始图像。)

NOTE :如果你想要一个“裁剪”来返回一个矩形内的图像部分,你可以用一个矩形将其blit到一个新的表面或复制一个子表面。

pygame.transform.laplacian()
find edges in a surface
laplacian(surface, dest_surface=None) -> Surface

使用拉普拉斯算法查找曲面中的边。

New in pygame 1.8.

pygame.transform.average_surfaces()
find the average surface from many surfaces.
average_surfaces(surfaces, dest_surface=None, palette_colors=1) -> Surface

获取一系列曲面,并返回每个曲面具有平均颜色的曲面。

Palette_Colors-如果为True,我们平均调色板中的颜色,否则我们平均像素值。如果曲面实际上是灰度颜色,而不是调色板颜色,则此选项非常有用。

注意,此函数当前不能正确处理使用曲面的调色板。

New in pygame 1.8.

New in pygame 1.9: palette_colors 论据

pygame.transform.average_color()
finds the average color of a surface
average_color(surface, rect=None, consider_alpha=False) -> Color

查找由RECT指定的曲面或曲面区域的平均颜色,并将其作为颜色返回。如果Consider_Alpha设置为True,则会考虑Alpha(移除黑色瑕疵)。

New in pygame 2.1.2: consider_alpha 论据

pygame.transform.threshold()
finds which, and how many pixels in a surface are within a threshold of a 'search_color' or a 'search_surf'.
threshold(dest_surface, surface, search_color, threshold=(0,0,0,0), set_color=(0,0,0,0), set_behavior=1, search_surf=None, inverse_set=False) -> num_threshold_pixels

这个多用途的函数可用于在‘surf’中查找与‘search_color’相近的颜色,或在单独的‘search_surf’中查找颜色。

它还可以用来将匹配或不匹配的像素传输到‘DEST_SURF’中。

默认情况下,它设置‘DEST_SURF’中的像素,其中不在阈值内的所有像素都更改为SET_COLOR。如果可选地将INVERSE_SET设置为True,则阈值内的像素将更改为SET_COLOR。

如果给出了可选的‘Search_Surf’图面,则使用它来设置阈值,而不是指定的‘set_color’。也就是说,它将在‘surf’中找到与‘Search_surf’相同坐标的像素的‘阈值’内的每个像素。

参数
返回类型

int

返回

与“Search_COLOR”或“Search_COLOR”相比,位于“SURF”中的“阈值”内的像素数 search_surf 。

示例

有关完整的示例,请参阅阈值测试:https://github.com/pygame/pygame/blob/master/test/transform_test.py

    def test_threshold_dest_surf_not_change(self):
        """the pixels within the threshold.

        All pixels not within threshold are changed to set_color.
        So there should be none changed in this test.
        """
        (w, h) = size = (32, 32)
        threshold = (20, 20, 20, 20)
        original_color = (25, 25, 25, 25)
        original_dest_color = (65, 65, 65, 55)
        threshold_color = (10, 10, 10, 10)
        set_color = (255, 10, 10, 10)

        surf = pygame.Surface(size, pygame.SRCALPHA, 32)
        dest_surf = pygame.Surface(size, pygame.SRCALPHA, 32)
        search_surf = pygame.Surface(size, pygame.SRCALPHA, 32)

        surf.fill(original_color)
        search_surf.fill(threshold_color)
        dest_surf.fill(original_dest_color)

        # set_behavior=1, set dest_surface from set_color.
        # all within threshold of third_surface, so no color is set.

        THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR = 1
        pixels_within_threshold = pygame.transform.threshold(
            dest_surface=dest_surf,
            surface=surf,
            search_color=None,
            threshold=threshold,
            set_color=set_color,
            set_behavior=THRESHOLD_BEHAVIOR_FROM_SEARCH_COLOR,
            search_surf=search_surf,
        )

        # # Return, of pixels within threshold is correct
        self.assertEqual(w * h, pixels_within_threshold)

        # # Size of dest surface is correct
        dest_rect = dest_surf.get_rect()
        dest_size = dest_rect.size
        self.assertEqual(size, dest_size)

        # The color is not the change_color specified for every pixel As all
        # pixels are within threshold

        for pt in test_utils.rect_area_pts(dest_rect):
            self.assertNotEqual(dest_surf.get_at(pt), set_color)
            self.assertEqual(dest_surf.get_at(pt), original_dest_color)

New in pygame 1.8.

Changed in pygame 1.9.4: 修复了许多错误并添加了关键字参数。测试您的代码。




Edit on GitHub