[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

Code: Select all

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

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

image = pygame.image.load('/home/metulburr/Pictures/bomb.jpeg').convert_alpha()

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

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
User avatar
Posts: 2058
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.

New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Posts: 1627
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest