Pygame Transform圖像變形的實現(xiàn)示例
發(fā)布日期:2021-12-10 05:14 | 文章來源:源碼之家
pygame.transform 模塊允許您對加載、創(chuàng)建后的圖像進行一系列操作,比如調(diào)整圖像大小、旋轉(zhuǎn)圖片等操作,常用方法如下所示:
下面看一組簡單的演示示例:
import pygame
#引入pygame中所有常量,比如 QUIT
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500,250))
pygame.display.set_caption('c語言中文網(wǎng)')
#加載一張圖片(455*191)
image_surface = pygame.image.load("C:/Users/Administrator/Desktop/c-net.png").convert()
image_new = pygame.transform.scale(image_surface,(300,300))
# 查看新生成的圖片的對象類型
#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()
實現(xiàn)示例
import pygame
pygame.init()
screen = pygame.display.set_mode((960, 600))
pygame.display.set_caption("圖像變換")
img = pygame.image.load('馬.jpg')
clock = pygame.time.Clock()
img1=pygame.transform.flip(img,False, True) #圖像進行水平和垂直翻轉(zhuǎn)
#參數(shù)1:要翻轉(zhuǎn)的圖像
#參數(shù)2:水平是否翻轉(zhuǎn)
#參數(shù)3:垂直是否翻轉(zhuǎn)
#返回一個新圖像
while True:
t = clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
screen.blit(img1,(100,50))
pygame.display.update()
img1 = pygame.transform.scale(img, (200, 100)) #縮放 #參數(shù)2:新圖像的寬高
img1 = pygame.transform.smoothscale(img,(400,300)) #平滑縮放圖像 #此函數(shù)僅適用于24位或32位surface。 如果輸入表面位深度小于24,則拋出異常
img1 = pygame.transform.scale2x(img) #快速的兩倍大小的放大
img = pygame.image.load('馬.jpg')
img1 = pygame.transform.rotate(img, 30) #旋轉(zhuǎn)圖像
#參數(shù)2:要旋轉(zhuǎn)的角度--正數(shù)表示逆時針--負數(shù)表示順時針
#除非以90度的增量旋轉(zhuǎn),否則圖像將被填充得更大的尺寸。 如果圖像具有像素alpha,則填充區(qū)域?qū)⑹峭该鞯?#旋轉(zhuǎn)是圍繞中心
img1 = pygame.transform.rotozoom(img, 30.0, 2.0) #縮放+旋轉(zhuǎn) #第一個參數(shù)指定要處理的圖像,第二個參數(shù)指定旋轉(zhuǎn)的角度數(shù),第三個參數(shù)指定縮放的比例 #這個函數(shù)會對圖像進行濾波處理,圖像效果會更好,但是速度會慢很多
img1 = pygame.transform.chop(img, (0, 0, 100, 50)) #對圖像進行裁減 #第一個參數(shù)指定要裁減的圖像,第二個參數(shù)指定要保留的圖像的區(qū)域
img = pygame.image.load('馬.jpg')
img1 = pygame.transform.laplacian(img) #查找邊--輪廓
以上就是Pygame Transform圖像變形的實現(xiàn)示例的詳細內(nèi)容,更多關(guān)于Pygame Transform圖像變形的資料請關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信