TypeError: img should be PIL Image.Got class ‘numpy.ndarray’

TypeError: img should be PIL Image. Got <class ‘numpy.ndarray’>

  • 问题分析
    当我们在使用
    transforms.RandomHorizontalFlip(p=0.5),
    transforms.RandomVerticalFlip(p=0.5),
    transforms.Resize((224,224)) 等函数的时候,这时候会报错:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/lib/python3.7/site-packages/torchvision/transforms/transforms.py", line 175, in __call__
    return F.resize(img, self.size, self.interpolation)
  File "/opt/conda/lib/python3.7/site-packages/torchvision/transforms/functional.py", line 189, in resize
    raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
TypeError: img should be PIL Image. Got <class 'numpy.ndarray'>
  • 解决办法

因为这些函数的输入应该是PIL Image的格式,其他的格式会报错,这时候我们应该把格式转为Image格式再进行,改为:

from PIL import Image
PIL_image = Image.fromarray(ndarray_image)   #这里ndarray_image为原来的numpy数组类型的输入

这样就可以进行下面的步骤了。

  • 22
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值