You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jupyter运行Python代码报错:AttributeError: type object 'Image' has no attribute 'fromarray'

解决AttributeError: type object 'Image' has no attribute 'fromarray'错误

问题原因

你代码里调用的Image.fromarray(),所引用的Image类来自Wand库(你用Wand做PDF转图片操作),但Wand的Image类并没有fromarray()方法。而pytesseract需要依赖PIL/Pillow库中Image类的fromarray()方法,将numpy数组转换为图片对象,因此触发该错误。

解决方案

  • 区分Wand和PIL的Image类导入
    同时使用两个库时,给Wand的Image指定别名,避免和PIL的Image命名冲突:

    # 导入Wand的Image并设置别名
    from wand.image import Image as WandImage
    # 导入PIL的Image类
    from PIL import Image
    

    后续代码里的Image.fromarray()会正确调用PIL的方法,Wand相关操作则使用WandImage即可。

  • 修改现有导入语句
    如果你之前直接导入了Wand的Image:from wand.image import Image,可以替换成上述带别名的导入方式;或者在调用fromarray()时明确指定PIL的Image:

    text = pytesseract.image_to_string(PIL.Image.fromarray(text_region))
    

    注意前提是已经导入import PIL。

内容的提问来源于stack exchange,提问作者rk987321

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 17:57:35