Python 如何使用SRCALPHA模式在pygame中将alpha设置为画布?
我创建了一个类来处理图像,在SRCALPHA模式下使用带有pygame曲面的“surface”变量。如何设置附加的alpha 我的代码是:Python 如何使用SRCALPHA模式在pygame中将alpha设置为画布?,python,image-processing,pygame,pygame-surface,Python,Image Processing,Pygame,Pygame Surface,我创建了一个类来处理图像,在SRCALPHA模式下使用带有pygame曲面的“surface”变量。如何设置附加的alpha 我的代码是: #!/usr/bin/env python # -*- coding: utf-8 -*- import pygame from pygame.locals import * from OpenGL.GL import * from OpenGL.GLU import * import Image import os class ImageManipu
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import Image
import os
class ImageManipulation:
def loadPicture(self, imagePath):
# http://stackoverflow.com/questions/38665920/convert-pil-image-into-pygame-surface-image
# http://stackoverflow.com/questions/29662908/how-do-i-blit-an-image-to-a-surface-in-pygame-thats-the-same-size-as-a-rectangl
# http://stackoverflow.com/questions/28005641/how-to-add-a-background-image-into-pygame
image = Image.open(imagePath)
mode = image.mode
size = image.size
data = image.tobytes()
py_image = pygame.image.fromstring(data, size, mode)
self.width = py_image.get_rect()[2]
self.height = py_image.get_rect()[3]
surface = pygame.Surface((self.width, self.height), pygame.SRCALPHA, 32)
surface = surface.convert_alpha()
surface.blit(py_image, py_image.get_rect())
self.surface = surface
def setAlpha(self, opacity):
# howto made this?
使用self.set\u alpha(不透明度)
将层与transform()函数混合时不起作用,生成填充背景,但不保留透明背景
我已经加载了一个完全透明的部分按钮,需要设置50%的不透明的所有按钮,并保留背景透明度。示例:我找到了使用特殊标志的解决方案:
def styleSetAlpha(self, opacity):
alpha_img = pygame.Surface((self.width, self.height), pygame.SRCALPHA)
alpha_img.fill((255, 255, 255, opacity))
self.surface.blit(alpha_img, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
资料来源: