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.
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:
With the ImageMagick utility instead, it's as easy as running this on the command line:
convert original.png -transparent white converted.png
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With