python - 类型错误 : unsupported operand type(s) for/: 'Image' and 'int'

标签 python python-3.x numpy python-imaging-library

我想将 PIL 图像对象转换为一个 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变量与存储crop<的image变量不同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 对象。如果您查看另一个存储裁剪图像并稍后传递给 np.arrayimage 变量,则此变量是 Image 的对象> 类(class):

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.

关于python - 类型错误 : unsupported operand type(s) for/: 'Image' and 'int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54011487/

相关文章:

python - 为什么我无法使用 Selenium 在 Python 中将 keys 发送到 Twitch 搜索栏?

Python 3.x : AttributeError: 'str' object has no attribute 'append'

python - 正确保存 numpy 数组

python - 从具有索引和值的矢量数据(字典)创建列表的函数

python - 在 flask 中使用 Gevent : API is not asynchronous

Python加载 'utf-16'文件无法解码 '\u0153'

python - 向数据库发送信息时出错

python - 如何平滑函数的导数?

python(numpy) -- 生成数组的另一种方法(从另一个数组)

在另一个方法中调用具有大量参数的方法的 Pythonic 方式