【问题标题】:Howto set alpha to canvas in pygame with SRCALPHA mode?如何使用 SRCALPHA 模式在 pygame 中将 alpha 设置为画布?
【发布时间】:2017-04-22 16:48:55
【问题描述】:

我创建了一个类来操作图像,在 SRCALPHA 模式下有一个带有 pygame 表面的“表面”变量。如何设置额外的 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 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) 不起作用,当使用 transform() 函数混合图层时,会填充背景但不保留透明背景。

我已经加载了一个带有完全透明部分的按钮,需要为所有按钮设置 50% 的不透明度并保留背景透明度。示例:http://www.clipartbest.com/cliparts/eTM/y5L/eTMy5LzAc.png

【问题讨论】:

    标签: python image-processing pygame pygame-surface


    【解决方案1】:

    我找到了使用特殊标志的解决方案:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多