Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save namieluss/c3fcec0b11d28e5d479c441ece1e7429 to your computer and use it in GitHub Desktop.

Select an option

Save namieluss/c3fcec0b11d28e5d479c441ece1e7429 to your computer and use it in GitHub Desktop.
Replace a Color (RGB) from an Image using Python Pillow
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()
@bcrivercity54
Copy link
Copy Markdown

if item[0] in list(range(190, 256)):

How does one determine this range? Thanks!

@namieluss
Copy link
Copy Markdown
Author

if item[0] in list(range(190, 256)):

How does one determine this range? Thanks!

Hello there,
In this example, I get the first value i.e item[0] of the tuple e.g. RGB(190, 240, 200).
Hence, item[0] = 190, which in range of my condition.
Then I replace that pixel with yellow i.e RGB(255, 204, 100).

This condition can change, it depends on what colour in image i.e test_image.jpg you want to replace. I attached a short sample using the exact code. I hope that's clear!

Screen.Recording.2021-06-14.at.10.27.50.AM.mov

@fschatbot
Copy link
Copy Markdown

In this example, I get the first value i.e item[0] of the tuple e.g. RGB(190, 240, 200).
Hence, item[0] = 190, which in range of my condition.
Then I replace that pixel with yellow i.e RGB(255, 204, 100).

Can you please be more specific or show the code as how we are suppose to do that

@InputBlackBoxOutput
Copy link
Copy Markdown

InputBlackBoxOutput commented Aug 11, 2021

@fschatbot Use MS paint's colour picker or use GIMP and guess the colour range. There is no definite method to determine the range.

@Underscore-163
Copy link
Copy Markdown

Underscore-163 commented May 15, 2026

hi, I'm struggling to get this to work. I assume that datas is a list of tuples and therefore item is a tuple and item[0] is the first item in that tuple. however, when I run this code, datas is a list of intergers so I get a type error because item is an int object and therefore not subscriptable. any help would be appriciated as I am new to PIL. thanks!

@Underscore-163
Copy link
Copy Markdown

hi, I'm struggling to get this to work. I assume that datas is a list of tuples and therefore item is a tuple and item[0] is the first item in that tuple. however, when I run this code, datas is a list of intergers so I get a type error because item is an int object and therefore not subscriptable. any help would be appriciated as I am new to PIL. thanks!

sorry, shortly after creating this comment i fixed it. turns out on line 4 I was doing img.convert("RGB") not img=img.convert("RGB"). appologies!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment