pygame.transform 模塊允許您對加載、創建后的圖像進行一系列操作,比如調整圖像大小、旋轉圖片等操作,常用方法如下所示:
下面看一組簡單的演示示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import pygame #引入pygame中所有常量,比如 QUIT from pygame. locals import * pygame.init() screen = pygame.display.set_mode(( 500 , 250 )) pygame.display.set_caption( 'c語言中文網' ) #加載一張圖片(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)) # 對新生成的圖像進行旋轉至45度 image_1 = pygame.transform.rotate(image_new, 45 ) # 使用rotozoom() 旋轉 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() |
實現示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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 ) #圖像進行水平和垂直翻轉 #參數1:要翻轉的圖像 #參數2:水平是否翻轉 #參數3:垂直是否翻轉 #返回一個新圖像 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() |
1
2
|
img1 = pygame.transform.scale(img, ( 200 , 100 )) #縮放 #參數2:新圖像的寬高 |
1
2
|
img1 = pygame.transform.smoothscale(img,( 400 , 300 )) #平滑縮放圖像 #此函數僅適用于24位或32位surface。 如果輸入表面位深度小于24,則拋出異常 |
1
|
img1 = pygame.transform.scale2x(img) #快速的兩倍大小的放大 |
1
2
3
4
5
|
img = pygame.image.load( '馬.jpg' ) img1 = pygame.transform.rotate(img, 30 ) #旋轉圖像 #參數2:要旋轉的角度--正數表示逆時針--負數表示順時針 #除非以90度的增量旋轉,否則圖像將被填充得更大的尺寸。 如果圖像具有像素alpha,則填充區域將是透明的 #旋轉是圍繞中心 |
1
2
3
|
img1 = pygame.transform.rotozoom(img, 30.0 , 2.0 ) #縮放+旋轉 #第一個參數指定要處理的圖像,第二個參數指定旋轉的角度數,第三個參數指定縮放的比例 #這個函數會對圖像進行濾波處理,圖像效果會更好,但是速度會慢很多 |
1
2
|
img1 = pygame.transform.chop(img, ( 0 , 0 , 100 , 50 )) #對圖像進行裁減 #第一個參數指定要裁減的圖像,第二個參數指定要保留的圖像的區域 |
1
2
|
img = pygame.image.load( '馬.jpg' ) img1 = pygame.transform.laplacian(img) #查找邊--輪廓 |
以上就是Pygame Transform圖像變形的實現示例的詳細內容,更多關于Pygame Transform圖像變形的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.cnblogs.com/liming19680104/p/13223908.html