[Pygame] transparent bg from image

[Pygame] transparent bg from image

Postby metulburr » Sun Apr 14, 2013 5:14 pm

So the images with (mostly oftbe) a white background. I normally use GIMP and go in and add an alpha and manuelly change what i want to be transparent. and then i just display the image. So i guess you can auto do this with pygame?

The two ways:

Code: Select all
Surface.convert_alpha()

and
Code: Select all
Surface.set_colorkey((255,0,255))


But using eitherscheme in an example with a few different pics it does not work. The white backgrounds of all the images still get drawn.
Code: Select all
import pygame

pygame.init()
screen = pygame.display.set_mode((500,500))

image = pygame.image.load('/home/metulburr/Pictures/bomb.jpeg').convert_alpha()
#image.set_colorkey((255,0,255))


run = True
while run:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         run = False
   screen.blit(image, (0,0))
   pygame.display.flip()



EDIT: oh i am stupid, setting the colorkey is setting the color that you want to be transparent? lmao OK so that would be 255,255,255 for white. But doing so gives a choppy background, some transparent pixels and some not. I could only assume that the pics bg is not all pure white at 255,255,255, so i would have to go in an edit it to alpha anyways right? which would defeat the purpose of this whole thing.

EDIT2: It almost seems easier just to go into gimp and convert manuelly the background to transparent. Then chop the image, crop it, and set it as a name and be done with? I dont get the reasoning behind all this complex methods for creating games, unless you were trying to mimic a program like GIMP.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1564
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: transparent bg from image

Postby Mekire » Sun Apr 14, 2013 11:42 pm

Yeah, I'm afraid when using color keys it will only make parts that are exactly that color transparent. If you have an image with a background that is almost uniform though it really shouldn't take much more than 30 seconds to fix. Just open the image in photoshop/paintdotnet/gimp(never used so not sure) magic wand the area and change it to the color you want to be transparent (preferably magenta :P). The only real reason you would have to use actual transparency is if you want to blit a semi-transparent image over another and you want the original image colors to show through (like with a tint). Since python is "slow" it is good to save performance where you can find it.

-Mek
User avatar
Mekire
 
Posts: 1149
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests