Forked from namieluss/python-pillow-image-change-background.py
Created
November 13, 2021 03:25
-
-
Save abdasis/3396f3c22ce4af23ee90fb629ea2f260 to your computer and use it in GitHub Desktop.
Replace a Color (RGB) from an Image using Python Pillow
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 | |
img = Image.open("test_image.jpg") | |
img = img.convert("RGB") | |
datas = img.getdata() | |
new_image_data = [] | |
for item in datas: | |
# change all white (also shades of whites) pixels to yellow | |
if item[0] in list(range(190, 256)): | |
new_image_data.append((255, 204, 100)) | |
else: | |
new_image_data.append(item) | |
# update image data | |
img.putdata(new_image_data) | |
# save new image | |
img.save("test_image_altered_background.jpg") | |
# show image in preview | |
img.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment