简体   繁体   English

'tuple'对象不支持项目分配

[英]'tuple' object does not support item assignment

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' object does not support item assignment

PIL pixels are tuples, and tuples are immutable. PIL像素是元组,元组是不可变的。 You need to construct a new tuple. 你需要构造一个新的元组。 So, instead of the for loop, do: 所以,而不是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. 此外,如果像素已经太红,则添加20将溢出该值。 You probably want something like min(pixel[0] + 20, 255) or int(255 * (pixel[0] / 255.) ** 0.9) instead of pixel[0] + 20 . 您可能想要像min(pixel[0] + 20, 255)int(255 * (pixel[0] / 255.) ** 0.9)而不是pixel[0] + 20

And, to be able to handle images in lots of different formats, do image = image.convert("RGB") after opening the image. 并且,为了能够处理许多不同格式的image = image.convert("RGB") ,请在打开图像后执行image = image.convert("RGB") The convert method will ensure that the pixels are always (r, g, b) tuples. convert方法将确保像素始终为(r,g,b)元组。

The second line should have been pixels[0] , with an S. You probably have a tuple named pixel , and tuples are immutable. 第二行应该是pixels[0] ,带有S.你可能有一个名为pixel的元组,元组是不可变的。 Construct new pixels instead: 改为构造新像素:

image = 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. python中的元组不能改变它们的值。 If you'd like to change the contained values though I suggest using a list: 如果您想更改包含的值,我建议使用列表:

[1,2,3] not (1,2,3) [1,2,3]不是(1,2,3)

您可能希望为您的像素进行下一次转换:

pixels = map(list, image.getdata())

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])

You have misspelt the second pixels as pixel . 您将第二个pixels拼错为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. 看来,由于拼写错误,你试图意外地修改一些名为pixel元组,而在Python中,元组是不可变的。 Hence the confusing error message. 因此令人困惑的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:“元组”对象不支持项目分配 - TypeError: 'tuple' object does not support item assignment Python-&#39;tuple&#39;对象不支持项目分配 - Python - 'tuple' object does not support item assignment 'tuple' object 不支持项目分配 - DataFrame - 'tuple' object does not support item assignment - DataFrame TypeError:“元组”对象不支持项目分配在非元组对象上 - TypeError: 'tuple' object does not support item assignment On non tuple object 元组作为字典的键说:“元组”对象不支持项目分配 - tuple as key to a dictionary says: 'tuple' object does not support item assignment 列表 object 上的“TypeError: 'tuple' object 不支持项目分配” - “TypeError: 'tuple' object does not support item assignment” on a list object 如何修复:TypeError &#39;tuple&#39; 对象不支持项目分配 - How to fix a : TypeError 'tuple' object does not support item assignment “元组”对象不支持查找局部最大值时的项目分配 - 'tuple' object does not support item assignment in finding local maximum 收到此错误-&#39;tuple&#39;对象不支持项目分配 - Getting this error - 'tuple' object does not support item assignment TypeError:“元组”对象不支持字典中的项目分配 - TypeError: 'tuple' object does not support item assignment in dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM