本文介绍了类型错误:“int"对象不可下标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的 PIL 代码时,它有这个错误:

when I run my PIL code ,it has this error:

from PIL import Image,ImageDraw, ImageColor, ImageChops

# Load images
im1 = Image.open('im1.png')
im2 = Image.open('im2.png')


# Flood fill white edges of image 2 with black
seed  = (0, 0)
black = ImageColor.getrgb("black")
ImageDraw.floodfill(im2, seed, black, thresh=127)

# Now select lighter pixel of image1 and image2 at each pixel location and
result = ImageChops.lighter(im1, im2)
result.save('result.png')

错误出在我的图像处理中:

the error is in my image processing:

Traceback (most recent call last):
File "C:\Users\Martin
Ma\Desktop\test\36\light_3_global\close_open\gray\main.py", line 96, in <module>
ImageDraw.floodfill(im2, seed, black, thresh=127)
File "E:\python\lib\site-packages\PIL\ImageDraw.py", line 346, in floodfill
if _color_diff(value, background) <= thresh:
File "E:\python\lib\site-packages\PIL\ImageDraw.py", line 386, in _color_diff
return abs(rgb1[0]-rgb2[0]) +  abs(rgb1[1]-rgb2[1]) +  abs(rgb1[2]-rgb2[2])
TypeError: 'int' object is not subscriptable

输入链接描述在这里

我该如何解决?非常感谢!

how can I solve it? thanks a lot !

推荐答案

您在未考虑后果的情况下更改了图像类型.JPEG 和 PNG 是根本不同的野兽,您需要注意这一点:

You have changed image type without thinking about the consequences. JPEG and PNG are fundamentally different beasts, and you need to be aware of that:

  • JPEG 图像是有损保存的,因此您的数据通常不会以您写入的相同值读回 - 这似乎让每个人都感到震惊.他们对图像进行阈值处理,使所有高于 127 的值变为白色,其他值变为黑色并具有真正的二值图像,然后他们将保存为 JPEG 并惊讶地发现,在重新加载时,尽管已对其进行了阈值处理,但该图像仍有 78 种颜色.

  • JPEG images are lossily saved, so your data will not generally be read back with the same values you wrote - this seems to shock everyone. They threshold an image so that all values above 127 go white and others go black and have a true binary image, they then save as JPEG and are amazed that on reloading, the image has 78 colours despite having thresholded it.

JPEG 图像具有各种伪像 - 大块的噪声会扰乱您的处理 - 特别是如果您查看饱和度.

JPEG images have all sorts of artefacts - chunky blocks of noise which will mess up your processing - especially if you look at saturation.

PNG 图像通常是调色板,其中每个像素将索引存储到 256 色调色板中,而不是 RGB 三元组.大多数操作在调色图像上都会失败,因为您将索引与 RGB 颜色三元组进行比较.

PNG images are often palettised where each pixel stores an index into a 256-colour palette, rather than an RGB triplet. Most operations will fail on palettised images because you are comparing an index with an RGB colour triplet.

PNG 图像通常是灰度的 - 因此只有一个通道,与 RGB 三元组的比较将失败,因为通道数量不同.

PNG images are often greyscale - so there is only one channel and comparisons with RGB triplets will fail because the number of channels differs.

因此,在回答您的问题时,我怀疑您的 PNG 图像已调色(尤其是当它只有 2 种颜色时).因此,您需要在打开时将其转换为 RGB 或亮度模式:

So, in answer to your question, I suspect your PNG image is palettised (especially likely when it only has 2 colours). You therefore need to convert it to RGB or maybe Luminance mode on opening:

im1 = Image.open('im1.png').convert('RGB')

这篇关于类型错误:“int"对象不可下标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:50