Created
February 26, 2018 14:46
-
-
Save fpg1503/18fdcfc3fdb793570d2c71cac056dec7 to your computer and use it in GitHub Desktop.
Remove image background and leave only white pixels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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