from PIL import Imageimage =\"balloon.jpg\")pixels = list(image.getdata())for pixel in pixels: pixel<0> = pixel<0> + 20 image.putdata(pixels)\"new.bmp\")However I gain this error: TypeError: \"tuple\" object does not support item assignment



PIL pixels space tuples, and also tuples are immutable. You need to construct a brand-new tuple. So, rather of the because that loop, do:

pixels = <(pixel<0> + 20, pixel<1>, pixel<2>) for pixel in pixels>image.putdata(pixels)Also, if the pixel is currently too red, including 20 will certainly overflow the value. You probably want something choose min(pixel<0> + 20, 255) or int(255 * (pixel<0> / 255.) ** 0.9) instead of pixel<0> + 20.

You are watching: Tuple object does not support item assignment

And, to be able to handle images in too many of various formats, do picture = image.convert(\"RGB\") after opened the image. The convert an approach will ensure the the pixels are constantly (r, g, b) tuples.


The 2nd line should have been pixels<0>, with an S. Friend probably have actually a tuple called pixel, and tuples room immutable. Construct new pixels instead:

image =\"balloon.jpg\")pixels = <(pix<0> + 20,) + pix<1:> for pix in image.getdata()>image.putdate(pixels)


Tuples, in python can\"t have their worths changed. If you\"d choose to readjust the had values despite I imply using a list:

<1,2,3> not (1,2,3)


A tuple is immutable and also thus you get the error friend posted.

pixel = (pixel<0> + 20, pixel<1>, pixel<2>)
You have misspelt the second pixels as pixel. The complying with works:

pixels = <1,2,3>pixels<0> = 5It appears that due to the typo you to be trying come accidentally modify some tuple referred to as pixel, and also in Python tuples room immutable. Therefore the confound error message.

Thanks because that contributing response to stack Overflow!

Please be sure to answer the question. Administer details and also share her research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; ago them increase with references or personal experience.

See more: Religious Happy Birthday Faith Images For Faith Instant Download

To learn more, view our tips on writing an excellent answers.

article Your prize Discard

By clicking “Post her Answer”, girlfriend agree come our regards to service, privacy policy and cookie plan

Not the answer you're looking for? Browse other questions tagged python python-imaging-library or ask your own question.

site architecture / logo design © 2021 ridge Exchange Inc; user contributions license is granted under cc by-sa. Rev2021.9.24.40305

your privacy

By clicking “Accept all cookies”, girlfriend agree ridge Exchange deserve to store cookie on your an equipment and disclose information in accordance through our Cookie Policy.