admin管理员组文章数量:1794759
PYGAME
图像转换
为了是pygame高效的使用图片需要调用convert()进行转换.
Pygame.image.load().convert() --- 像素格式的转换(不是surface对象的转换)
在每次blit的时候(图片盖在另一图片上)的时候python会自动强制进行像素格式转换,效率低不如先转换
Pygame.image.convert_alpha(200) --- 包含透明的需要使用alpha解析,jpg不支持透明,通常用png和gif,转换后的图片只能用pixal alpha修改
Pygame 三种透明方式:set_colorkey(#RGB) / set_alpha(200 整体做alpha) / pixal alpha(每个像素都有alpha通道)
import pygame
import sys
from pygame.locals import *pygame.init()
size = width, height = 1000, 800
bg = (0, 0, 0)screen = pygame.display.set_mode(size)
pygame.display.set_caption('Color Convert Test')
clock = pygame.time.Clock()minions = pygame.image.load('minions.jpg').convert_alpha()
bg_pic = pygame.image.load('bg.jpg').convert()
position = minions.get_rect()
position.center = width//2, height//2# method1 - setcolorkey
#minions.set_colorkey((255,255,255))
# method2 - set_alpha(0-200) for opacity
#minions.set_alpha(200)# method3 - set_at for pixl alpha
'''
for i in range(position.width):for j in range(position.height):temp = minions.get_at((i, j))if temp[3] != 0:temp[3] = 200minions.set_at((i, j), temp)
'''
# method4 - build a transparent screen with pic and then paste back to target screen
def blit_opac(target, source, location, opacity):x = location[0]y = location[1]temp = pygame.Surface((source.get_width(), source.get_height())).convert()temp.blit(target, (-x, -y))temp.blit(source, (0, 0))temp.set_alpha(opacity)target.blit(temp, position)while True:for event in pygame.event.get():if event.type == QUIT:sys.exit()screen.blit(bg_pic, (0,0))#screen.blit(minions, position)blit_opac(screen, minions, position, 200)pygame.display.flip()clock.tick(30)
本文标签: pygame
版权声明:本文标题:PYGAME 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1695288280a305636.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论