Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyGame: Applying transparency to an image with alpha?

I want to display an image with alpha with a specified transparency, but can't figure out how to do it.

To elaborate on how I'm struggling with this, the blurb below is a slightly modified hunk of code from this SO answer, but if you run it, you'll see that "image" loses it's native alpha, while the alpha of "image2" never changes! Yuck.

#!/usr/bin/env python

import pygame, sys

pygame.init()

window = pygame.display.set_mode((200, 200))
background = pygame.Surface((window.get_size()))
background.fill((255, 255, 255))
image = image2 = pygame.image.load('alpha.png')

image = image.convert()
rect = image.get_rect()

image2 = image2.convert_alpha()
rect2 = image2.get_rect()

rect2.left = rect.width + 1

i = 0
while True:
  for event in pygame.event.get():
    if event.type == 12:
      pygame.quit()
      sys.exit()

  image.set_alpha(i)
  image2.set_alpha(i)

  window.fill((255, 255, 255))
  window.blit(background, background.get_rect())
  window.blit(image, rect)
  window.blit(image2, rect2)

  if i == 255:
    i = 0
  else:
    i += 1

  pygame.display.update()
  pygame.time.Clock().tick(60)

So ... how is it done?

like image 632
Synthead Avatar asked Oct 14 '12 04:10

Synthead


People also ask

How do I make an image transparent in pygame?

By calling the convert() method of an (image-) surface instance with no arguments passed, Pygame converts the image surface to the same format as your main display surface. This is recommended, because it is faster to draw or blit images which have the same pixel format (depth, flags etc.)

What does convert alpha do in pygame?

pygame.Surface.convert_alpha. — change the pixel format of an image including per pixel alphas.

What is Get_rect () in pygame?

The method get_rect() returns a Rect object from an image. At this point only the size is set and position is placed at (0, 0). We set the center of the Rect to the center of the screen: rect = img. get_rect() rect.


2 Answers

Make a copy of the image you want to show (to not change the original) and use following:

self.image = self.original_image.copy()
# this works on images with per pixel alpha too
alpha = 128
self.image.fill((255, 255, 255, alpha), None, pygame.BLEND_RGBA_MULT)

Sorry, to not provide a full example.

like image 66
DR0ID Avatar answered Sep 28 '22 09:09

DR0ID


For what it's worth, I've seen varying results depending on the image itself. In my situation, I found that using Gimp to change the image allowed me to keep the code simple:

image = pygame.image.load('path/to/my.png').convert()
image.set_alpha(128)
surface.blit(image, (0, 0))

Using that code, the following sprites just don't work right: http://www.spriters-resource.com/resources/sheets/47/50365.png

However, it does work after using Gimp to change 2 things:

  1. Image -> Mode -> Convert to Color Profile... and use the defaults
  2. Image -> Mode -> Indexed... and use the defaults

After saving the changes, it works for me in Ubuntu. I can't post an image of the result because I'm a new user...

like image 22
moorecm Avatar answered Sep 28 '22 08:09

moorecm