2017-03-17 65 views
0

當我嘗試在我的新編輯的圖像上放置邊框時,會出現此錯誤。錯誤來自這行代碼。類型錯誤'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)) 

謝謝。

回答

0

由於錯誤是在ImageOps.expand,在解釋這條命令:

help(ImageOps.expand) 

它會告訴你如何使用功能,這應該幫助你瞭解一下吧誤解。

即,根據錯誤判斷,特別是left, top, right, bottom = _border(border),它看起來好像試圖從border參數中獲得4個值。這也是爲什麼它無法從邊界獲得項目,即運行方法__getitem__

基本上,您需要將border參數更改爲4種東西的列表 - 左側,頂部,右側和底部邊框尺寸。

+0

謝謝。我現在會嘗試。 – DZapza

+0

我回來了......我怎樣才能把邊界變成4個東西的列表? – DZapza

+0

@DZapza它需要返回一個列表:'返回左邊,頂部,右邊,底部' – Barmar

相關問題