簡體   English   中英

類型錯誤'int'對象沒有屬性'__getitem__'

[英]Type Error 'int' object has no attribute '__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種內容的列表-左,上,右和下邊框大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM