亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Pygame?transform模塊入門介紹

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

簡介

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

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

下面看一組簡單的演示示例:

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))
# 對新生成的圖像進行旋轉(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()

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

函數(shù)說明

  • pygame.transform.flip()

翻轉(zhuǎn)Surface

flip(Surface,xbool,ybool)->Surface

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

  • pygame.transform.scale()

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

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

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

  • pygame.transform.rotate()

rotate(surface, angle) -> Surface

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

  • pygame.transform.rotozoom()

rotozoom(surface, angle, scale) -> Surface

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

  • pygame.transform.scale2x()

scale2x(surface, dest_surface=None) -> Surface

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

  • pygame.transform.smoothscale()

平滑地縮放一個任意大小的Surface。

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

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

  • pygame.transform.get_smoothscale_backend()

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

get_smoothscale_backend() -> string

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

  • pygame.transform.set_smoothscale_backend()

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

set_smoothscale_backend(backend) -> None

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

  • pygame.transform.chop()

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

chop(surface, rect) -> Surface

提取圖像的一部分。刪除給定矩形區(qū)域周圍的所有垂直和水平像素。然后將角區(qū)域(矩形的對角線)合并。此操作不會改變原始圖像)。NOTE :如果需要“裁切”來返回rect中圖像的一部分,則可以使用rect剪切到新Surface或復(fù)制次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-如果為真, 我們對調(diào)色板中的顏色進行平均, 否則我們對像素值進行平均。如果Surface實際上是灰度顏色, 而不是調(diào)色板顏色, 這很有用。注意, 這個函數(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

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

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

Parameters:

dest_surf(pygame.Surface或**None)-我們正在更改的Surface。參見“ set_behavior”。如果計數(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和藍色閾值2。

set_color(pygame.Color或**None)-我們在dest_surf中設(shè)置的顏色。

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

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

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

Return type:int

Returns:

‎與“search_color‎‎”或search_surf‎‎相比,“surf”中“閾值”內(nèi)的像素數(shù)。‎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)

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

相關(guān)文章

最新評論