'tuple' object does not support item assignment
Solution 1:
PIL pixels are tuples, and tuples are immutable. You need to construct a new tuple. So, instead of the for loop, do:
pixels = [(pixel[0] + 20, pixel[1], pixel[2]) for pixel in pixels]
image.putdata(pixels)
Also, if the pixel is already too red, adding 20 will overflow the value. You probably want something like min(pixel[0] + 20, 255)
or int(255 * (pixel[0] / 255.) ** 0.9)
instead of pixel[0] + 20
.
And, to be able to handle images in lots of different formats, do image = image.convert("RGB")
after opening the image. The convert method will ensure that the pixels are always (r, g, b) tuples.
Solution 2:
The second line should have been pixels[0]
, with an S. You probably have a tuple named pixel
, and tuples are immutable. Construct new pixels instead:
image = Image.open('balloon.jpg')
pixels = [(pix[0] + 20,) + pix[1:] for pix in image.getdata()]
image.putdate(pixels)