Pygame transform模块

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Pygame transform模块

acktomas   2022-09-28 我要评论

简介

pygame模块用于变换Surface,Surface变换是一种移动或调整像素大小的操作。所有这些函数都是对一个Surface进行操作, 并将结果返回一个新的Surface。

有些变换被认为是破坏性的。这意味着每次执行这些变换都会丢失像素数据。常见的例子是调整大小和旋转。出于这个原因, 重新变换原始Surface比继续多次变换图像要好。(例如, 假设您正在制作一个弹跳弹簧的动画, 它将膨胀和收缩。如果您将尺寸变化逐步应用于之前的图像, 您将失去细节。相反, 总是从原始图像开始, 然后缩放到所需的大小。)

下面看一组简单的演示示例:

import pygame
#引入pygame中所有常量,比如 QUIT
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500,250))
pygame.display.set_caption('pygame transform')
#加载一张图片(455*191)
image_surface = pygame.image.load("sky_shoot/img/playerShip1_orange.png").convert()
image_new = pygame.transform.scale(image_surface,(100,75))
# 查看新生成的图片的对象类型
#print(type(image_new))
# 对新生成的图像进行旋转至45度
image_1 =pygame.transform.rotate(image_new,45)
# 使用rotozoom() 旋转 0 度,将图像缩小0.5倍
image_2 = pygame.transform.rotozoom(image_1,0,0.5)
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
    # 将最后生成的image_2添加到显示屏幕上
    screen.blit(image_2,(0,0))
    pygame.display.update()

程序的运行结果如下:

函数说明

  • pygame.transform.flip()

翻转Surface

flip(Surface,xbool,ybool)->Surface

此功能可以垂直、水平或同时翻转一个Surface。翻转Surface是非破坏性的, 并返回一个具有相同尺寸的新Surface。

  • pygame.transform.scale()

调整到新的分辨率

scale(Surface, (width, height), DesSurface=None)->Surface

将Surface调整为新的分辨率。这是一个不对结果进行采样的快速缩放操作。可以使用一个可选的目标Surface, 而不是让它创建一个新的Surface。如果你想重复缩放一些东西, 这样会更快。但是, 目标Surface的大小必须与传入的(宽、高)相同。而且目标Surface必须是相同的格式。

  • pygame.transform.rotate()

rotate(surface, angle) -> Surface

未经过滤的逆时针旋转。角度参数代表度数, 可以是任何浮点值。负的角度值将顺时针旋转。除非以90度为增量旋转, 否则图像将被垫大以保持新的尺寸。如果图像有象素, 填充区域将是透明的。否则, pygame将选择与Surface colorkey或topleft像素值相匹配的颜色。

  • pygame.transform.rotozoom()

rotozoom(surface, angle, scale) -> Surface

这是一个结合了缩放和旋转的变换。得到的Surface是一个过滤后的32位Surface。刻度参数是一个浮点数, 将乘以当前的分辨率。角度参数是一个浮点值, 代表要旋转的逆时针度数。负的旋转角度将顺时针旋转。

  • pygame.transform.scale2x()

scale2x(surface, dest_surface=None) -> Surface

这将返回一个新的图像, 其大小是原来的两倍。它使用AdvanceMAME Scale2X算法, 对位图图形进行 "无锯齿 "缩放。这实际上只对纯色的简单图像有影响。在摄影和反锯齿图像上, 它将看起来像一个普通的未经过滤的规模。可以使用一个可选的目标Surface, 而不是让它创建一个新的Surface。如果您想重复缩放某些Surface, 这样做会更快。但是目标Surface的大小必须是传入的源Surface的两倍。而且目标Surface必须是相同的格式。

  • pygame.transform.smoothscale()

平滑地缩放一个任意大小的Surface。

smoothscale(surface, size, dest_surface=None) -> Surface

使用两种不同的算法之一来按需缩放输入Surface的每个尺寸。为了缩小,输出像素是它们覆盖的颜色的面积平均值。为了扩展,使用双线性滤波器。对于x86-64和i686体系结构,包含了优化的 MMX 例程,它们的运行速度比其他计算机类型快得多。大小是2个数字的序列(宽度,高度)。此功能仅适用于24位或32位Surface。如果输入Surface位深度小于24,则将引发异常。pygame 1.8中的新功能。

  • pygame.transform.get_smoothscale_backend()

返回正在使用的smoothscale滤波器版本。‘GENERIC’, ‘MMX’, 或 ‘SSE’。

get_smoothscale_backend() -> string

显示平滑缩放是否正在使用 MMXSSE 加速。如果没有可用的加速度,则返回“ GENERIC”。对于x86处理器,要在运行时确定要使用的加速级别。该函数用于pygame的测试和调试。

  • pygame.transform.set_smoothscale_backend()

将平滑尺度过滤器版本设置为:“GENERIC”、"MMX "或 "SSE "之一。‘GENERIC’, ‘MMX’, 或 ‘SSE’.

set_smoothscale_backend(backend) -> None

设置平滑比例加速度。采用字符串参数。值“ GENERIC”将关闭加速。“ MMX” 仅使用 MMX 指令。'SSE’也允许 SSE 扩展。如果当前处理器无法识别或不支持类型,则会引发值错误。这个函数是为了pygame测试和调试而提供的。如果 smoothscale 导致无效指令错误, 那么这是一个 pygame/SDL 错误, 应该被报告。仅将此函数用作临时修复。

  • pygame.transform.chop()

获取一个去掉内部区域的图像副本。

chop(surface, rect) -> Surface

提取图像的一部分。删除给定矩形区域周围的所有垂直和水平像素。然后将角区域(矩形的对角线)合并。此操作不会改变原始图像)。NOTE :如果需要“裁切”来返回rect中图像的一部分,则可以使用rect剪切到新Surface或复制次Surface。

  • pygame.transform.laplacian()

寻边

laplacian(surface, dest_surface=None) -> Surface

使用大写字母算法来寻找Surface的边缘。pygame 1.8中的新功能。

  • pygame.transform.average_surfaces()

从许多Surface中找出平均Surface。

average_surfaces(surfaces, dest_surface=None, palette_colors=1) -> Surface

取一个Surface的序列, 并返回每个Surface的平均颜色的Surface。palette_colors-如果为真, 我们对调色板中的颜色进行平均, 否则我们对像素值进行平均。如果Surface实际上是灰度颜色, 而不是调色板颜色, 这很有用。注意, 这个函数目前不能正确处理使用Surface的调色板。pygame 1.8中的新功能。pygame 1.9的新功能: palette_colors 参数

  • pygame.transform.average_color()

找到Surface的平均颜色

average_color(surface, rect=None) -> Color

查找Surface或矩形指定的Surface区域的平均颜色, 并以颜色返回。

  • pygame.transform.threshold()

查找Surface中哪些像素和多少像素在 "search_color "或 "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

这个多功能的函数可以用于在接近 "search_color "的 "surf "中查找颜色, 或者接近单独的 "search_surf "中的颜色。它也可以用来将匹配或不匹配的像素转移到’dest_surf’中。默认情况下, 它将’dest_surf’中所有不在阈值内的像素都改为set_color。

如果inverse_set设置为True, 则在阈值内的像素会被改变为set_color。如果给定了可选的’search_surf’Surface, 它被用来针对而不是指定的’set_color’进行阈值化。也就是说, 它将在’surf’中找到每一个在’search_surf’相同坐标的像素的’阈值’内的像素。

Parameters:

dest_surf(pygame.Surface或**None)-我们正在更改的Surface。参见“ set_behavior”。如果计数(set_behavior为0),则应为None。

surf(pygame.Surface)-我们正在查看的Surface。

search_color(pygame.Color)-我们正在寻找的颜色。

threshold(pygame.Color)-在距search_color(或search_surf)距离之内。您可以使用阈值(r,g,b,a),其中r,g,b可以具有不同的阈值。因此,您可以根据需要使用r阈值40和蓝色阈值2。

set_color(pygame.Color或**None)-我们在dest_surf中设置的颜色。

set_behavior(int)-set_behavior=1 (默认)。dest_surface中的像素将被改为’set_color’。set_behavior=0 我们不改变’dest_surf’, 只是计数。让dest_surf=None。set_behavior=2在’dest_surf’中设置的像素将从’surf’。

search_surf(pygame.Surface或**None)-search_surf=None(默认)。用’search_color’代替搜索。search_surf=Surface。看’search_surf’中的颜色, 而不是使用’search_color’。

inverse_set(bool)-False, 默认值。阈值外的像素会被改变。True, 在阈值内的像素被改变。

Return type:int

Returns:

‎与“search_color‎‎”或search_surf‎‎相比,“surf”中“阈值”内的像素数。‎search_surf.

Examples:请参阅阈值测试以获取完整的示例

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)

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们