栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

PYGAME - image convert

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PYGAME - image convert

图像转换

为了是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] = 200
        minions.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)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/769847.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号