Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a command in PIL that is like the reverse operation to getdata()

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading