Python pygame實現(xiàn)圖像基本變換的示例詳解
函數(shù)列表
pygame的transform中封裝了一些基礎(chǔ)的圖像處理函數(shù),列表如下
| 函數(shù) | 功能 |
|---|---|
| flip | 鏡像 |
| scale | 縮放至新的分辨率 |
| scale_by | 根據(jù)因子進行縮放 |
| 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來對圖像進行縮放,如果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在縮放的基礎(chǔ)上,進行了雙邊濾波平滑,使得縮放看上去更加自然,
旋轉(zhuǎn)
通過rotate可以對圖像進行旋轉(zhuǎn),其輸入?yún)?shù)有二,分別是待旋轉(zhuǎn)圖像與旋轉(zhuǎ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實現(xiàn)圖像基本變換的示例詳解的文章就介紹到這了,更多相關(guān)Python pygame圖像變換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Python爬蟲錯誤之twisted.web.error.SchemeNotSupported: Unsu
這篇文章主要介紹了解決Python爬蟲錯誤之twisted.web.error.SchemeNotSupported: Unsupported scheme: b''問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
Python 中開發(fā)pattern的string模板(template) 實例詳解
這篇文章主要介紹了Python 中開發(fā)pattern的string模板(template) 實例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
PyTorch之nn.ReLU與F.ReLU的區(qū)別介紹
這篇文章主要介紹了PyTorch之nn.ReLU與F.ReLU的區(qū)別介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python SQLite3數(shù)據(jù)庫日期與時間常見函數(shù)用法分析
這篇文章主要介紹了Python SQLite3數(shù)據(jù)庫日期與時間常見函數(shù)用法,結(jié)合實例形式分析了Python連接、查詢SQLite3數(shù)據(jù)以及數(shù)據(jù)庫日期與時間常見操作方法,需要的朋友可以參考下2017-08-08
Python字符編碼轉(zhuǎn)碼之GBK,UTF8互轉(zhuǎn)
說到python的編碼,一句話總結(jié),說多了都是淚啊,這個在以后的python的開發(fā)中絕對是一件令人頭疼的事情。所以有必要輸入理解2020-02-02

