2017-04-22 39 views
0

我做了一個類來操縱圖像,在SRCALPHA模式下有一個pygame表面的「表面」變量。如何設置額外的alpha ?.如何在pygame中使用SRCALPHA模式將alpha設置爲canvas?

我的代碼是:

#!/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_alpha(opacity)不工作,當與交融變換()函數層使填充背景,但不保存透明背景。

我已經加載了一個帶有全透明部分的按鈕,需要爲所有按鈕設置不透明度的50%並保留背景透明度。例如:http://www.clipartbest.com/cliparts/eTM/y5L/eTMy5LzAc.png

回答

0

我發現使用特殊標誌的解決方案:

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) 

來源:PyGame: translucent sprites with per pixel alpha

相關問題