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

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

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

函數(shù)列表

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

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

翻轉

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

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則根據給定的factor來對圖像進行縮放,如果factor是一個數(shù)字,那么將對兩個方向進行同樣的縮放,如果是一個元組,比如 ( 3 , 4 ) (3,4) (3,4),那么對兩個維度分別縮放3倍和4倍。示例如下

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

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

旋轉

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

代碼如下

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()

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

相關文章

最新評論