Skip to content

Instantly share code, notes, and snippets.

@fpg1503
Created February 26, 2018 14:46
Show Gist options
  • Save fpg1503/18fdcfc3fdb793570d2c71cac056dec7 to your computer and use it in GitHub Desktop.
Save fpg1503/18fdcfc3fdb793570d2c71cac056dec7 to your computer and use it in GitHub Desktop.
Remove image background and leave only white pixels
from PIL import Image
image = Image.open('img.png').convert("RGBA")
pixels = image.getdata()
new_pixels = list(map(lambda item: (255, 255, 255, 0) if item[0] < 250 and item[1] < 250 and item[2] < 250 else item, pixels))
image.putdata(new_pixels)
image.save("img2.png", "PNG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment