5

I have been trying to create an image processing program to take all the white pixels (255,255,255) in a loaded image and set their alpha channels to 0 (non-opaque), and then save the image.

I've been using Python's pygame extension to help me achieve this, but so far I cannot find a simple way to do what I just described above.

Keep in mind I'm not trying to display an image, I'm trying to manipulate it.

asheeshr
  • 4,088
  • 6
  • 31
  • 50
Sigma Freud
  • 57
  • 1
  • 3
  • 1
    Do you have to use pygame? PIL is well suited for this. See http://stackoverflow.com/questions/765736/using-pil-to-make-all-white-pixels-transparent – Tim Dec 30 '12 at 01:32

1 Answers1

2

I also suggest using PIL or ImageMagick, but here is a way to do it in pygame:

import pygame

def convert():    
    pygame.init()
    pygame.display.set_mode()
    image = pygame.image.load("triangle.png").convert_alpha()
    for x in range(image.get_width()):
        for y in range(image.get_height()):
            if image.get_at((x, y)) == (255, 255, 255, 255):
                image.set_at((x, y), (255, 255, 255, 0))
    pygame.image.save(image, "converted.png")

if __name__ == "__main__":
    convert()

The above works for a white background. Here's how triangle.png and converted.png look using magenta instead of white so you can see the difference:

magenta bg transparent bg

With the ImageMagick utility instead, it's as easy as running this on the command line:

convert original.png -transparent white converted.png
0eggxactly
  • 4,642
  • 1
  • 16
  • 16