ZBLOG

raise TypeError(f"pic should be PIL Image or ndarray. Got {type(pic)}") TypeError: pic should be PIL Image or ndarray. Got 是什么错误,该怎么改

这个错误意味着变量’pic’的类型为’int’,但是应该是一个PIL Image对象或numpy ndarray。因此,代码无法处理给定的输入。

要修复此错误,您需要确保传递给函数的输入是PIL Image对象或numpy ndarray之一。可以通过在将其传递给函数之前检查输入变量的类型并根据需要将其转换为正确的类型来实现这一点。

例如,如果您想要接受整数作为输入,您可以将整数转换为PIL图像或ndarray。以下是一个将整数转换为PIL图像的示例:

  1. from PIL import Image
  2. import numpy as np
  3. def process_image(pic):
  4. if isinstance(pic, int):
  5. # Convert integer to grayscale PIL Image object
  6. pic = np.full((100, 100), pic, dtype=np.uint8)
  7. pic = Image.fromarray(pic, mode='L')
  8. elif not isinstance(pic, (Image.Image, np.ndarray)):
  9. raise TypeError(f"pic should be PIL Image or ndarray. Got {type(pic)}")
  10. # Rest of the code for processing PIL Image or ndarray
  11. return pic

上面的代码将整数转换为灰度PIL Image对象,然后继续对其进行处理。

请注意,上述示例仅适用于将整数转换为PIL图像。如果您希望将其他类型的输入转换为图像或ndarray,请相应地更改代码。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=2459

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?