简体   繁体   English

TypeError: 'int' object is not subscriptable 错误

[英]TypeError: 'int' object is not subscriptable error

I am facing a problem in my code.我在我的代码中遇到问题。 Pycharm tells me that I have the error: "TypeError: 'int' object is not subscriptable" at the line: acc[0] = acc[0] + (pixel[0] * kernel[a][b]) . Pycharm 告诉我错误: "TypeError: 'int' object is not subscriptable"在行: acc[0] = acc[0] + (pixel[0] * kernel[a][b]) Anyone knows how to solve it?任何人都知道如何解决它?

from PIL import Image, ImageDraw
from numpy import asarray
# Load image:
input_image = Image.open("Cameraman.tif")
input_pixels = input_image.load()

# Box Blur kernel
box_kernel = ([[-1, -1, -1],
                   [-1, 8, -1],
                   [-1, -1, -1]])


# Select kernel here:
kernel = box_kernel

# Middle of the kernel
offset = len(kernel) // 2

# Create output image
output_image = Image.new("RGB", input_image.size)
draw = ImageDraw.Draw(output_image)

# Compute convolution between intensity and kernels
output_image = Image.new("RGB", input_image.size)
draw = ImageDraw.Draw(output_image)

# Compute convolution with kernel
for x in range(offset, input_image.width - offset):
    for y in range(offset, input_image.height - offset):
        acc = [0, 0, 0]
        for a in range(len(kernel)):
            for b in range(len(kernel)):
                xn = x + a - offset
                yn = y + b - offset
                pixel = input_pixels[xn, yn]
                acc[0] = acc[0] + (pixel[0] * kernel[a][b])
                acc[1] = acc[1] + (pixel[1] * kernel[a][b])
                acc[2] = acc[2] + (pixel[2] * kernel[a][b])

        draw.point((x, y), (int(acc[0]), int(acc[1]), int(acc[2])))

output_image.save("Filtered.png")

img1arr = asarray(input_image)
img2arr = asarray(output_image)
im1arrF = img1arr.astype('float')
im2arrF = img2arr.astype('float')
additionF = (im1arrF+im2arrF)/2
addition = additionF.astype('uint8')
resultImage = Image.fromarray(addition)
resultImage.save('Sharpened.jpg')

Given that acc and kernel are defined in your code and have enough dimensions to support the indexing the cuplrit must be pixel .鉴于acckernel在您的代码中定义并且具有足够的维度来支持索引,因此 cuplrit 必须是pixel I assume you're expecting a one-dimensional length-3 array representing the RGB values at point [xn,yn] .我假设您期望一个长度为 3 的一维数组表示点[xn,yn]处的 RGB 值。 The most obvious possibility here is that you've loaded a grayscale image - could that be the case?这里最明显的可能性是您加载了灰度图像 - 可能是这种情况吗?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM