欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pygame?transform模塊入門(mén)介紹

 更新時(shí)間:2022年08月08日 10:24:14   作者:acktomas  
pygame.transform模塊允許您對(duì)加載、創(chuàng)建后的圖像進(jìn)行一系列操作,比如調(diào)整圖像大小、旋轉(zhuǎn)圖片等操作,感興趣的可以了解一下

簡(jiǎn)介

pygame模塊用于變換Surface,Surface變換是一種移動(dòng)或調(diào)整像素大小的操作。所有這些函數(shù)都是對(duì)一個(gè)Surface進(jìn)行操作, 并將結(jié)果返回一個(gè)新的Surface。

有些變換被認(rèn)為是破壞性的。這意味著每次執(zhí)行這些變換都會(huì)丟失像素?cái)?shù)據(jù)。常見(jiàn)的例子是調(diào)整大小和旋轉(zhuǎn)。出于這個(gè)原因, 重新變換原始Surface比繼續(xù)多次變換圖像要好。(例如, 假設(shè)您正在制作一個(gè)彈跳彈簧的動(dòng)畫(huà), 它將膨脹和收縮。如果您將尺寸變化逐步應(yīng)用于之前的圖像, 您將失去細(xì)節(jié)。相反, 總是從原始圖像開(kāi)始, 然后縮放到所需的大小。)

下面看一組簡(jiǎn)單的演示示例:

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))
# 查看新生成的圖片的對(duì)象類(lèi)型
#print(type(image_new))
# 對(duì)新生成的圖像進(jìn)行旋轉(zhuǎn)至45度
image_1 =pygame.transform.rotate(image_new,45)
# 使用rotozoom() 旋轉(zhuǎn) 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()

程序的運(yùn)行結(jié)果如下:

函數(shù)說(shuō)明

  • pygame.transform.flip()

翻轉(zhuǎn)Surface

flip(Surface,xbool,ybool)->Surface

此功能可以垂直、水平或同時(shí)翻轉(zhuǎn)一個(gè)Surface。翻轉(zhuǎn)Surface是非破壞性的, 并返回一個(gè)具有相同尺寸的新Surface。

  • pygame.transform.scale()

調(diào)整到新的分辨率

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

將Surface調(diào)整為新的分辨率。這是一個(gè)不對(duì)結(jié)果進(jìn)行采樣的快速縮放操作??梢允褂靡粋€(gè)可選的目標(biāo)Surface, 而不是讓它創(chuàng)建一個(gè)新的Surface。如果你想重復(fù)縮放一些東西, 這樣會(huì)更快。但是, 目標(biāo)Surface的大小必須與傳入的(寬、高)相同。而且目標(biāo)Surface必須是相同的格式。

  • pygame.transform.rotate()

rotate(surface, angle) -> Surface

未經(jīng)過(guò)濾的逆時(shí)針旋轉(zhuǎn)。角度參數(shù)代表度數(shù), 可以是任何浮點(diǎn)值。負(fù)的角度值將順時(shí)針旋轉(zhuǎn)。除非以90度為增量旋轉(zhuǎn), 否則圖像將被墊大以保持新的尺寸。如果圖像有象素, 填充區(qū)域?qū)⑹峭该鞯?。否則, pygame將選擇與Surface colorkey或topleft像素值相匹配的顏色。

  • pygame.transform.rotozoom()

rotozoom(surface, angle, scale) -> Surface

這是一個(gè)結(jié)合了縮放和旋轉(zhuǎn)的變換。得到的Surface是一個(gè)過(guò)濾后的32位Surface。刻度參數(shù)是一個(gè)浮點(diǎn)數(shù), 將乘以當(dāng)前的分辨率。角度參數(shù)是一個(gè)浮點(diǎn)值, 代表要旋轉(zhuǎn)的逆時(shí)針度數(shù)。負(fù)的旋轉(zhuǎn)角度將順時(shí)針旋轉(zhuǎn)。

  • pygame.transform.scale2x()

scale2x(surface, dest_surface=None) -> Surface

這將返回一個(gè)新的圖像, 其大小是原來(lái)的兩倍。它使用AdvanceMAME Scale2X算法, 對(duì)位圖圖形進(jìn)行 "無(wú)鋸齒 "縮放。這實(shí)際上只對(duì)純色的簡(jiǎn)單圖像有影響。在攝影和反鋸齒圖像上, 它將看起來(lái)像一個(gè)普通的未經(jīng)過(guò)濾的規(guī)模。可以使用一個(gè)可選的目標(biāo)Surface, 而不是讓它創(chuàng)建一個(gè)新的Surface。如果您想重復(fù)縮放某些Surface, 這樣做會(huì)更快。但是目標(biāo)Surface的大小必須是傳入的源Surface的兩倍。而且目標(biāo)Surface必須是相同的格式。

  • pygame.transform.smoothscale()

平滑地縮放一個(gè)任意大小的Surface。

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

使用兩種不同的算法之一來(lái)按需縮放輸入Surface的每個(gè)尺寸。為了縮小,輸出像素是它們覆蓋的顏色的面積平均值。為了擴(kuò)展,使用雙線性濾波器。對(duì)于x86-64和i686體系結(jié)構(gòu),包含了優(yōu)化的 MMX 例程,它們的運(yùn)行速度比其他計(jì)算機(jī)類(lèi)型快得多。大小是2個(gè)數(shù)字的序列(寬度,高度)。此功能僅適用于24位或32位Surface。如果輸入Surface位深度小于24,則將引發(fā)異常。pygame 1.8中的新功能。

  • pygame.transform.get_smoothscale_backend()

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

get_smoothscale_backend() -> string

顯示平滑縮放是否正在使用 MMXSSE 加速。如果沒(méi)有可用的加速度,則返回“ GENERIC”。對(duì)于x86處理器,要在運(yùn)行時(shí)確定要使用的加速級(jí)別。該函數(shù)用于pygame的測(cè)試和調(diào)試。

  • pygame.transform.set_smoothscale_backend()

將平滑尺度過(guò)濾器版本設(shè)置為:“GENERIC”、"MMX "或 "SSE "之一。‘GENERIC’, ‘MMX’, 或 ‘SSE’.

set_smoothscale_backend(backend) -> None

設(shè)置平滑比例加速度。采用字符串參數(shù)。值“ GENERIC”將關(guān)閉加速。“ MMX” 僅使用 MMX 指令。'SSE’也允許 SSE 擴(kuò)展。如果當(dāng)前處理器無(wú)法識(shí)別或不支持類(lèi)型,則會(huì)引發(fā)值錯(cuò)誤。這個(gè)函數(shù)是為了pygame測(cè)試和調(diào)試而提供的。如果 smoothscale 導(dǎo)致無(wú)效指令錯(cuò)誤, 那么這是一個(gè) pygame/SDL 錯(cuò)誤, 應(yīng)該被報(bào)告。僅將此函數(shù)用作臨時(shí)修復(fù)。

  • pygame.transform.chop()

獲取一個(gè)去掉內(nèi)部區(qū)域的圖像副本。

chop(surface, rect) -> Surface

提取圖像的一部分。刪除給定矩形區(qū)域周?chē)乃写怪焙退较袼?。然后將角區(qū)域(矩形的對(duì)角線)合并。此操作不會(huì)改變?cè)紙D像)。NOTE :如果需要“裁切”來(lái)返回rect中圖像的一部分,則可以使用rect剪切到新Surface或復(fù)制次Surface。

  • pygame.transform.laplacian()

尋邊

laplacian(surface, dest_surface=None) -> Surface

使用大寫(xiě)字母算法來(lái)尋找Surface的邊緣。pygame 1.8中的新功能。

  • pygame.transform.average_surfaces()

從許多Surface中找出平均Surface。

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

取一個(gè)Surface的序列, 并返回每個(gè)Surface的平均顏色的Surface。palette_colors-如果為真, 我們對(duì)調(diào)色板中的顏色進(jìn)行平均, 否則我們對(duì)像素值進(jìn)行平均。如果Surface實(shí)際上是灰度顏色, 而不是調(diào)色板顏色, 這很有用。注意, 這個(gè)函數(shù)目前不能正確處理使用Surface的調(diào)色板。pygame 1.8中的新功能。pygame 1.9的新功能: palette_colors 參數(shù)

  • pygame.transform.average_color()

找到Surface的平均顏色

average_color(surface, rect=None) -> Color

查找Surface或矩形指定的Surface區(qū)域的平均顏色, 并以顏色返回。

  • pygame.transform.threshold()

查找Surface中哪些像素和多少像素在 "search_color "或 "search_surf "的閾值之內(nèi)。

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

這個(gè)多功能的函數(shù)可以用于在接近 "search_color "的 "surf "中查找顏色, 或者接近單獨(dú)的 "search_surf "中的顏色。它也可以用來(lái)將匹配或不匹配的像素轉(zhuǎn)移到’dest_surf’中。默認(rèn)情況下, 它將’dest_surf’中所有不在閾值內(nèi)的像素都改為set_color。

如果inverse_set設(shè)置為T(mén)rue, 則在閾值內(nèi)的像素會(huì)被改變?yōu)閟et_color。如果給定了可選的’search_surf’Surface, 它被用來(lái)針對(duì)而不是指定的’set_color’進(jìn)行閾值化。也就是說(shuō), 它將在’surf’中找到每一個(gè)在’search_surf’相同坐標(biāo)的像素的’閾值’內(nèi)的像素。

Parameters:

dest_surf(pygame.Surface或**None)-我們正在更改的Surface。參見(jiàn)“ set_behavior”。如果計(jì)數(shù)(set_behavior為0),則應(yīng)為None。

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

search_color(pygame.Color)-我們正在尋找的顏色。

threshold(pygame.Color)-在距search_color(或search_surf)距離之內(nèi)。您可以使用閾值(r,g,b,a),其中r,g,b可以具有不同的閾值。因此,您可以根據(jù)需要使用r閾值40和藍(lán)色閾值2。

set_color(pygame.Color或**None)-我們?cè)赿est_surf中設(shè)置的顏色。

set_behavior(int)-set_behavior=1 (默認(rèn))。dest_surface中的像素將被改為’set_color’。set_behavior=0 我們不改變’dest_surf’, 只是計(jì)數(shù)。讓dest_surf=None。set_behavior=2在’dest_surf’中設(shè)置的像素將從’surf’。

search_surf(pygame.Surface或**None)-search_surf=None(默認(rèn))。用’search_color’代替搜索。search_surf=Surface???rsquo;search_surf’中的顏色, 而不是使用’search_color’。

inverse_set(bool)-False, 默認(rèn)值。閾值外的像素會(huì)被改變。True, 在閾值內(nèi)的像素被改變。

Return type:int

Returns:

‎與“search_color‎‎”或search_surf‎‎相比,“surf”中“閾值”內(nèi)的像素?cái)?shù)。‎search_surf.

Examples:請(qǐng)參閱閾值測(cè)試以獲取完整的示例

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)

到此這篇關(guān)于Pygame transform模塊入門(mén)介紹的文章就介紹到這了,更多相關(guān)Pygame transform模塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論