簡體   English   中英

TypeError:/不支持的操作數類型/:'Image'和'int'

[英]TypeError: unsupported operand type(s) for /: 'Image' and 'int'

我想將PIL Image對象轉換為numpy數組。 我嘗試使用以下代碼顯示錯誤

TypeError Traceback (most recent call last) <ipython-input-133-0898103f22f0> in <module>()
      1 image_path = 'test/28/image_05230.jpg'
----> 2 image = process_image(image_path)
      3 imshow(image)

<ipython-input-129-e036faebfd31> in process_image(image_path)
     24     # normalize
     25     print(type(image))
---> 26     image_arr = np.array(image) / 255
     27     mean = np.array([0.485, 0.456, 0.406])
     28     std_dv = np.array( [0.229, 0.224, 0.225])

TypeError: unsupported operand type(s) for /: 'Image' and 'int'

from PIL import Image

image = Image.open(image_path)
image = np.asarray(image) / 255

我也嘗試使用此代碼image = np.array(image)/ 255它顯示相同的錯誤。 (以下代碼)

from PIL import Image

image = Image.open(image_path)
image = np.array(image) / 255

僅當我在下面的函數中使用上面的代碼時才會發生此錯誤

def convert_pil_to_numpy_array(image_path):
    # Load Image an open the image
    from PIL import Image

    image = Image.open(image_path)
    width = image.size[0]
    height = image.size[1]

    if width > height:
      image.thumbnail((500, 256))
    else:
      image.thumbnail((256, 500))

    left_margin = (image.width - 224) / 2
    lower_margin = (image.height - 224) / 2
    upper_margin = lower_margin + 224
    right_margin = left_margin + 224

    image = image.crop((left_margin, upper_margin, right_margin, lower_margin))

    # normalize
    print(type(image))
    image_arr = np.array(image) / 255
    mean = np.array([0.485, 0.456, 0.406])
    std_dv = np.array( [0.229, 0.224, 0.225])
    image_arr = (image_arr - mean)/std_dv

    return image_arr

在函數convert_pil_to_numpy_array()時, image最初使用變量是從不同的image ,其存儲可變crop PED Image對象。

from PIL import Image
image_path = "C:\\temp\\Capture.JPG"
image = Image.open(image_path)
print(type(image))
#Output
<class 'PIL.JpegImagePlugin.JpegImageFile'>

這是一個JpegImageFile對象。 如果您查看存儲裁剪圖像的其他image變量,並稍后傳遞給np.array ,則此變量是Image類的對象:

image = image.crop((left_margin, upper_margin, right_margin, lower_margin))
print(type(image))
#Output:
<class 'PIL.Image.Image'>

問題在於傳遞給crop()函數的元組值。 使用傳遞給crop的邊距值,圖像無法轉換為數組並再次返回Image對象:

image_arr = np.array(image)
print(image_arr)
#Output:
<PIL.Image.Image image mode=RGB size=224x0 at 0x39E4F60>

由於你的圖像尺寸與我的不同,我使用了傳遞給crop()的4元組的不同值並得到了一個數組:

image = image.crop((50,100,60,120))
image_arr = np.array(image)
#Output:
  [[[-2.11790393 -2.03571429 -1.80444444]
  [-2.11790393 -2.03571429 -1.80444444]
  [-2.11790393 -2.03571429 -1.80444444]
  [-2.11790393 -2.03571429 -1.80444444]
  [-2.11790393 -2.03571429 -1.80444444]
  [-2.11790393 -2.03571429 -1.80444444]
  [-2.11790393 -2.03571429 -1.80444444]
  [-2.11790393 -2.03571429 -1.80444444]
  [-2.11790393 -2.03571429 -1.80444444]
  [-2.11790393 -2.03571429 -1.80444444]]..etc

你應該做的是,檢查邊距值並將裁剪的圖像保存到文件(jpg,png等),然后轉換為數組。 請注意,我沒有將保存的圖像存儲到任何變量。

image.crop((50, 60, 100, 120)).save("test.jpg")
image_arr = np.array(Image.open("test.jpg")) / 255
mean = np.array([0.485, 0.456, 0.406])
std_dv = np.array( [0.229, 0.224, 0.225])
image_arr = (image_arr - mean)/std_dv
print(image_arr)
#Output:
  [[[-0.04580872  0.08263305  0.30448802]
  [-0.91917116 -0.81022409 -0.58440087]
  [ 0.81042898  0.95798319  1.17594771]
  ...
  [ 2.19753404  2.37605042  2.58771242]
  [-0.02868396 -0.19747899  0.13019608]
  [-0.11430773 -0.28501401  0.04305011]]
  ....etc.

這有效:

from PIL import Image
import numpy as np

image = Image.open(r'C:\temp\2015-05-14 17.43.10.jpg') # path to existing local file
image_arr = np.asarray(image) / 255

print(image_arr)

輸出:

[[[ 0.35294118  0.39607843  0.41960784]
  [ 0.38039216  0.42352941  0.44705882]
  [ 0.41568627  0.45098039  0.47058824]
  ...,
  [ 0.05490196  0.04705882  0.05098039]
  [ 0.04705882  0.03921569  0.04313725]
  [ 0.04313725  0.03529412  0.03921569]]

 [[ 0.36470588  0.4         0.42745098]
  [ 0.38823529  0.42352941  0.44313725]
  [ 0.40784314  0.44313725  0.4627451 ]
  ..., etc ]

既然您已經呈現了實際使用的實際代碼:

  • Image.open("path.jpg")返回<class 'PIL.JpegImagePlugin.JpegImageFile'>
  • 裁剪完畢后,您將獲得<class 'PIL.Image.Image'>的返回

如果您檢查裁剪的image ,可以看到它只有一個維度,第二個是0:

調試器圖片

如果您將代碼修改為:

def convert_pil_to_numpy_array(image_path):
    # Load Image an open the image
    from PIL import Image

    image = Image.open(image_path)
    width = image.size[0]
    height = image.size[1] 

    image.thumbnail((500, 256) if (width > height) else (256, 500))  

    left_margin = (image.width - 224) / 2
    upper_margin = (image.height - 224) / 2     # fixed
    lower_margin = upper_margin + 224           # fixed
    right_margin = left_margin + 224

    # fixed and renamed so you do not overwrite image all the time - helps debugging
    # now this has 2 dimensions that are non-zero
    image_crop = image.crop((left_margin, upper_margin, right_margin, lower_margin))

    # normalize
    image_arr = np.asarray(image) / 255
    mean = np.mean(image_arr)
    std_dv = np.std( image_arr )
    image_arr = (image_arr - mean)/std_dv 

    return image_crop

代碼突然運行沒有錯誤。

暫無
暫無

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

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