本文介绍了类型错误:不支持/的操作数类型:'Image' 和 'int'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将 PIL Image 对象转换为一个 numpy 数组.我尝试使用以下代码显示错误
I wanted to convert the PIL Image object into a numpy array. I tried using the following codes it showing an error
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 它显示相同的错误.(代码如下)
I also tried with this code image = np.array(image) / 255 it's showing the same error. (code below)
from PIL import Image
image = Image.open(image_path)
image = np.array(image) / 255
仅当我在下面的函数中使用上述代码时才会出现此错误
This error occurs only when I used the above code in below function
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
推荐答案
既然您已经展示了您实际使用的真实代码:
Now that you presented the real code you are actually using:
Image.open("path.jpg")
返回- 在你裁剪之后你会得到
Image.open("path.jpg")
returns<class 'PIL.JpegImagePlugin.JpegImageFile'>
- after your cropping you get a return of
<class 'PIL.Image.Image'>
如果您检查裁剪后的图像
,您可以看到它只有一个维度,第二个维度为 0:
If you inspect your cropped image
, you can see it only has one dimension, the second is 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
代码突然运行没有错误.
the code suddenly runs without errors.
这篇关于类型错误:不支持/的操作数类型:'Image' 和 'int'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!