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

Python pygame實(shí)現(xiàn)圖像基本變換的示例詳解

 更新時間:2023年11月30日 10:33:35   作者:微小冷  
pygame的transform中封裝了一些基礎(chǔ)的圖像處理函數(shù),這篇文章主要為大家介紹了pygame實(shí)現(xiàn)圖像的基本變換,例如縮放、旋轉(zhuǎn)、鏡像等,感興趣的小伙伴可以了解一下

函數(shù)列表

pygame的transform中封裝了一些基礎(chǔ)的圖像處理函數(shù),列表如下

函數(shù)功能
flip鏡像
scale縮放至新的分辨率
scale_by根據(jù)因子進(jìn)行縮放
scale2x專業(yè)圖像倍增器
rotate旋轉(zhuǎn)
rotozoom縮放并旋轉(zhuǎn)
smoothscale平滑縮放
smoothscale_by平滑縮放至新的分辨率
chop獲取已刪除內(nèi)部區(qū)域的圖像的副本
laplacian邊緣查找
average_surfaces多個圖像求均值
average_color圖像顏色的均值
grayscale灰度化
threshold在某個閾值內(nèi)的像素個數(shù)

圖像顯示

為了演示這些功能效果,先通過pygame做一個顯示圖像的函數(shù)

import pygame

def showImg(img):
    pygame.init()
    rect = img.get_rect()
    screen = pygame.display.set_mode((rect.width, rect.height))
    while True:
        for e in pygame.event.get():
            if e.type==pygame.QUIT:
                pygame.quit()
                return
        screen.blit(img, rect)
        pygame.display.flip()


ball = pygame.image.load("intro_ball.gif")
showImg(ball)

翻轉(zhuǎn)

flip用于翻轉(zhuǎn),除了輸入圖像之外,還有兩個布爾型參數(shù),分別用于調(diào)控水平方向和豎直方向,True表示翻轉(zhuǎn),F(xiàn)alse表示不翻轉(zhuǎn)。

import pygame.transform as pt
xFlip = pt.flip(ball, True, False)
yFlip = pt.flip(ball, False, True)
xyFlip = pt.flip(ball, True, True)

效果分別如下

縮放

相對于鏡像來說,在游戲中,縮放顯然是更加常用的操作。在transform模塊中,提供了兩種縮放方案

scale(surface, size, dest_surface=None)

scale_by(surface, factor, dest_surface=None)

其中,scale將原圖像縮放至給定的size;scale_by則根據(jù)給定的factor來對圖像進(jìn)行縮放,如果factor是一個數(shù)字,那么將對兩個方向進(jìn)行同樣的縮放,如果是一個元組,比如 ( 3 , 4 ) (3,4) (3,4),那么對兩個維度分別縮放3倍和4倍。示例如下

xScale = pt.scale_by(ball, (2,1))
yScale = pt.scale(ball, (111,222))

此外,smoothscale和smoothscale_by在縮放的基礎(chǔ)上,進(jìn)行了雙邊濾波平滑,使得縮放看上去更加自然,

旋轉(zhuǎn)

通過rotate可以對圖像進(jìn)行旋轉(zhuǎn),其輸入?yún)?shù)有二,分別是待旋轉(zhuǎn)圖像與旋轉(zhuǎn)角度。這個功能可以無縫插入到前面的平拋運(yùn)動中,即讓球在平拋時有一個旋轉(zhuǎn),示例如下

代碼如下

import time

pygame.init()

size = width, height = 640, 320
speed = [10, 0]

screen = pygame.display.set_mode(size)

ball = pygame.image.load("intro_ball.gif")
rect = ball.get_rect()

th = 0
while True:
    if pygame.QUIT in [e.type for e in pygame.event.get()]:
        break
    time.sleep(0.02)
    rect = rect.move(speed)
    if rect.right>width:
        speed = [10, 0]
        rect = ball.get_rect()
    if rect.bottom>height:
        speed[1] = -speed[1]
    speed[1] += 1
    th += 5
    screen.fill("black")
    screen.blit(pt.rotate(ball, th), rect)
    pygame.display.flip()

pygame.quit()

到此這篇關(guān)于Python pygame實(shí)現(xiàn)圖像基本變換的示例詳解的文章就介紹到這了,更多相關(guān)Python pygame圖像變換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論