0

I make a class to manipulate images, have "surface" variable with a pygame surface in SRCALPHA mode. Howto set an additional alpha?.

My code is:

#!/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?

With self.set_alpha(opacity) does not work, when blend the layer with transform() function make a fill background but dont preserve the transparent background.

I have loaded a button with full transparent parts, need set the 50% of opacity to all button and preserve the background transparency. Example: http://www.clipartbest.com/cliparts/eTM/y5L/eTMy5LzAc.png

e-info128
  • 3,727
  • 10
  • 40
  • 57

1 Answers1

0

I found the solution using special flags:

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)

Source: PyGame: translucent sprites with per pixel alpha

Community
  • 1
  • 1
e-info128
  • 3,727
  • 10
  • 40
  • 57