So I have the following code that gets the rgb value of an image in a list like shown:
from PIL import Image
imageInput = Image.open("gradient.png")
r = list(imageInput.getdata())
print(r)
it returns r as an array, now, I have another code that alters that array to change the rgb value, is there a function in PIL that can feed the array to python so it can change the color of the image?
(I have digged through the PIL document and couldn’t find anything)
>Solution :
You are probably looking for putdata() method:
from PIL import Image
imageInput = Image.open("gradient.png")
# convert every tuple to a list:
r = [list(x) for x in imageInput.getdata()]
# Make your changes
for i in range(len(r)):
r[i][1] = 100
# After you are done editing, convert back to tuple:
r = tuple(tuple(x) for x in r)
imageInput.putdata(r)
imageInput.save("new_gradient.png")