I am using the PIL library.
我正在使用PIL库。
I am trying to make an image look red-er, this is what i've got.
我试图使图像看起来更红,这就是我所拥有的。
from PIL import Image
image = Image.open('balloon.jpg')
pixels = list(image.getdata())
for pixel in pixels:
pixel[0] = pixel[0] + 20
image.putdata(pixels)
image.save('new.bmp')
However I get this error: TypeError: 'tuple' object does not support item assignment
但是我收到此错误:TypeError:'tuple'对象不支持项目分配
6 个解决方案
#1
35
PIL pixels are tuples, and tuples are immutable. You need to construct a new tuple. So, instead of the for loop, do:
PIL像素是元组,元组是不可变的。你需要构造一个新的元组。所以,而不是for循环,做:
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
.
此外,如果像素已经太红,添加20将溢出该值。您可能需要像min(pixel [0] + 20,255)或int(255 *(pixel [0] / 255.)** 0.9)而不是像素[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.
并且,为了能够处理许多不同格式的图像,请在打开图像后执行image = image.convert(“RGB”)。 convert方法将确保像素始终是(r,g,b)元组。
#2
6
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:
第二行应该是像素[0],带有S.你可能有一个名为像素的元组,元组是不可变的。改为构造新像素:
image = Image.open('balloon.jpg')
pixels = [(pix[0] + 20,) + pix[1:] for pix in image.getdata()]
image.putdate(pixels)
#3
4
Tuples, in python can't have their values changed. If you'd like to change the contained values though I suggest using a list:
python中的元组不能改变它们的值。如果您想更改包含的值,我建议使用列表:
[1,2,3]
not (1,2,3)
[1,2,3]不是(1,2,3)
#4
3
You probably want the next transformation for you pixels:
您可能希望为您的像素进行下一次转换:
pixels = map(list, image.getdata())
#5
1
A tuple is immutable and thus you get the error you posted.
元组是不可变的,因此您会收到您发布的错误。
>>> pixels = [1, 2, 3]
>>> pixels[0] = 5
>>> pixels = (1, 2, 3)
>>> pixels[0] = 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
In your specific case, as correctly pointed out in other answers, you should write:
在您的具体情况下,正如其他答案中正确指出的那样,您应该写:
pixel = (pixel[0] + 20, pixel[1], pixel[2])
#6
0
You have misspelt the second pixels
as pixel
. The following works:
您将第二个像素拼错为像素。以下作品:
pixels = [1,2,3]
pixels[0] = 5
It appears 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.
看来,由于拼写错误,你试图意外地修改一些名为像素的元组,而在Python中,元组是不可变的。因此令人困惑的错误消息。
#1
35
PIL pixels are tuples, and tuples are immutable. You need to construct a new tuple. So, instead of the for loop, do:
PIL像素是元组,元组是不可变的。你需要构造一个新的元组。所以,而不是for循环,做:
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
.
此外,如果像素已经太红,添加20将溢出该值。您可能需要像min(pixel [0] + 20,255)或int(255 *(pixel [0] / 255.)** 0.9)而不是像素[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.
并且,为了能够处理许多不同格式的图像,请在打开图像后执行image = image.convert(“RGB”)。 convert方法将确保像素始终是(r,g,b)元组。
#2
6
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:
第二行应该是像素[0],带有S.你可能有一个名为像素的元组,元组是不可变的。改为构造新像素:
image = Image.open('balloon.jpg')
pixels = [(pix[0] + 20,) + pix[1:] for pix in image.getdata()]
image.putdate(pixels)
#3
4
Tuples, in python can't have their values changed. If you'd like to change the contained values though I suggest using a list:
python中的元组不能改变它们的值。如果您想更改包含的值,我建议使用列表:
[1,2,3]
not (1,2,3)
[1,2,3]不是(1,2,3)
#4
3
You probably want the next transformation for you pixels:
您可能希望为您的像素进行下一次转换:
pixels = map(list, image.getdata())
#5
1
A tuple is immutable and thus you get the error you posted.
元组是不可变的,因此您会收到您发布的错误。
>>> pixels = [1, 2, 3]
>>> pixels[0] = 5
>>> pixels = (1, 2, 3)
>>> pixels[0] = 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
In your specific case, as correctly pointed out in other answers, you should write:
在您的具体情况下,正如其他答案中正确指出的那样,您应该写:
pixel = (pixel[0] + 20, pixel[1], pixel[2])
#6
0
You have misspelt the second pixels
as pixel
. The following works:
您将第二个像素拼错为像素。以下作品:
pixels = [1,2,3]
pixels[0] = 5
It appears 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.
看来,由于拼写错误,你试图意外地修改一些名为像素的元组,而在Python中,元组是不可变的。因此令人困惑的错误消息。