from PIL import Imagepicture = Image.open("balloon.jpg")pixels = list(picture.getdata())for pixel in pixels: pixel<0> = pixel<0> + 20 picture.putdata(pixels)image.save("new.bmp")However before I obtain this error: TypeError: "tuple" object does not support item assignment
PIL pixels are tuples, and also tuples are immutable. You have to construct a new tuple. So, rather of the for loop, do:
pixels = <(pixel<0> + 20, pixel<1>, pixel<2>) for pixel in pixels>picture.putdata(pixels)Also, if the pixel is already too red, adding 20 will certainly overcirculation the worth. You most likely want something choose min(pixel<0> + 20, 255) or int(255 * (pixel<0> / 255.) ** 0.9) rather of pixel<0> + 20.
You are watching: Tuple object does not support item assignment
And, to have the ability to handle imeras in numerous different styles, carry out photo = picture.convert("RGB") after opening the image. The convert strategy will certainly encertain that the pixels are always (r, g, b) tuples.
The second line have to have actually been pixels<0>, through an S. You most likely have actually a tuple called pixel, and also tuples are immutable. Construct new pixels instead:
photo = Image.open("balloon.jpg")pixels = <(pix<0> + 20,) + pix<1:> for pix in image.getdata()>image.putdate(pixels)
Tuples, in python can"t have their values changed. If you"d prefer to readjust the consisted of worths though I indicate making use of a list:
<1,2,3> not (1,2,3)
A tuple is immutable and also for this reason you obtain the error you posted.
pixel = (pixel<0> + 20, pixel<1>, pixel<2>)
You have actually misspelt the second pixels as pixel. The complying with works:
pixels = <1,2,3>pixels<0> = 5It shows up that due to the typo you were trying to accidentally modify some tuple called pixel, and in Python tuples are immutable. Hence the confusing error message.
Thanks for contributing a response to Stack Overflow!Please be sure to answer the question. Provide details and also share your research!
But avoid …Asking for help, clarification, or responding to various other answers.Making statements based on opinion; earlier them up with referrals or individual endure.
See more: Fix Failed To Send Tweet Failed To Send To Twitter, Cannot Post Tweet On Twitter
To learn even more, check out our tips on writing excellent answers.
Article Your Answer Discard
Not the answer you're looking for? Browse other questions tagged python python-imaging-library or ask your own question.
site style / logo design © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.9.15.40218