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

问题描述:

当我尝试在我的新编辑的图像上放置边框时,会出现此错误。错误来自这行代码。类型错误'int'对象没有属性'__getitem__'

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

谢谢。

由于错误是在ImageOps.expand,在解释这条命令:

help(ImageOps.expand) 

它会告诉你如何使用功能,这应该帮助你了解一下吧误解。

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

基本上,您需要将border参数更改为4种东西的列表 - 左侧,顶部,右侧和底部边框尺寸。

+0

谢谢。我现在会尝试。 – DZapza

+0

我回来了......我怎样才能把边界变成4个东西的列表? – DZapza

+0

@DZapza它需要返回一个列表:'返回左边,顶部,右边,底部' – Barmar