类型错误'int'对象没有属性'__getitem__'

时间:2017-03-17 23:40:54

标签: python

当我尝试在新编辑的图像上放置边框时,会出现此错误。错误来自这行代码。

img_with_border = ImageOps.expand(imgred,border=25,fill='red')

有人有修复吗? (我是python的菜鸟。)

def family():
q1=raw_input('What is the directory of your image? : ')
q2=raw_input('Which of these best fits you?: Christian, Patriot, or Satanist? : ')
q3=raw_input('What is the width of your frame? : ')
filename = (q1)
img = plt.imread(filename)
# Read the image data into an array
imgred = plt.imread(filename)
img = plt.imread(filename)
imgpat = plt.imread(filename)
imgblue = plt.imread(filename)
imggreen = plt.imread(filename)
imggrey = plt.imread(filename)
height = len(imgred)
width = len(imgred[0])
if q2 == 'Satanist':
    for c1 in range(height):
        for c2 in range(width):
            imgred[c1][c2][1]=0
            imgred[c1][c2][2]=0
            r=abs(imgred[c1][c2][0]-255)
            g=abs(imggreen[c1][c2][1]-255)
            b=abs(imgblue[c1][c2][2]-255)
            imgred[c1][c2]=[r,g,b]
            r=abs(imgred[c1][c2][0]-255)
            g=abs(imggreen[c1][c2][1]-255)
            b=abs(imgblue[c1][c2][2]-255)
            imgred[c1][c2]=[r,g,b]




def give_image():
img_with_border = ImageOps.expand(imgred,border=25,fill='red')              
# Create figure with 3 subplots
fig, ax = plt.subplots(1, 3)
# Show the image data in a subplot
ax[0].imshow(img, interpolation='none')
ax[1].imshow(imgred, interpolation='none')
ax[2].imshow(img_with_border, interpolation='none')

MCVE: 当我尝试将边框添加到已编辑的图片时,会出现错误。 例如:

image_file = ('woman.jpg')
filename = 'C:/Users/Dylan/woman.jpg'
imgred = plt.imread(filename)
height = len(imgred)
width = len(imgred[0])

for c1 in range(height):
    for c2 in range(width):
        imgred[c1][c2][1]=0
        imgred[c1][c2][2]=0

def give_image():
img_with_border = ImageOps.expand(imgred,border=25,fill='red')              
# Create figure with 3 subplots
fig, ax = plt.subplots(1, 1)
# Show the image data in a subplot
ax.imshow(imgred, interpolation='none')

但是当它是未编辑的图片并且我尝试添加边框时,没有错误。

image_file = ('woman.jpg')
filename = 'C:/Users/Dylan/woman.jpg'
img = plt.imread(filename)
img = Image.open('woman.jpg')
img_with_border = ImageOps.expand(img,border=25,fill='violet')

def give_image():
    fig, ax = plt.subplots(1, 1)
    # Show the image data in a subplot
    ax.imshow(img_with_border, interpolation='none')

错误本身。

235     "Add border to image"
236     left, top, right, bottom = _border(border)
--> 237     width = left + image.size[0] + right
238     height = top + image.size[1] + bottom
239     out = Image.new(image.mode, (width, height), _color(fill, image.mode))

感谢。

1 个答案:

答案 0 :(得分:0)

由于错误位于ImageOps.expand,请在解释器中运行:

help(ImageOps.expand)

它会告诉你如何使用这个功能,这可以帮助你理解你对它的误解。

即,根据错误判断,特别是left, top, right, bottom = _border(border),看起来它试图从border参数中获取4个值。这也是它无法从边界获取项目的原因,即运行方法__getitem__

基本上你需要将border参数更改为4个事物的列表 - 左边,上边,右边和下边框大小。