I'm trying to replace a color range with some specific color using PIL module. Suppose I want to change the color of the sky,I need to generate a list of the color of pixels.
My code currently
from PIL import Image, ImageColor
im = Image.open("iphone.jpg", "r")
to_be_replaced = ImageColor.getcolor("#5f86c9", "RGB")
newcolor = ImageColor.getcolor("#c9bb5f", "RGB")
print(to_be_replaced, newcolor)
pix = im.load()
width, height = im.size
for w in range(width):
for h in range(height):
r, g, b = pix[(w, h)]
if (r, g, b) == to_be_replaced:
pix[w, h] = newcolor
else:
pix[w, h] = (r, g, b)
im.save('test.png', 'PNG')
newcolor
here is the yellow color, and to_be_replaced
is the color of a pixel from the sky.
Code output till now:
The script works, but currently, it changes the color of just a pixel with the specified color. I want to replace the color of the whole sky. Can anybody please help me with this problem?