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

使用EasyOCR时遇AttributeError:PIL.Image无ANTIALIAS属性问题求助

车牌检测系统EasyOCR调用问题解决指南

问题概述

基于OpenCV和YOLO开发车牌自动检测系统时,将numpy数组传入easyocr的readtext方法出现报错,后续降级Pillow版本后仍出现非法指令错误。

相关代码

import easyocr
reader = easyocr.Reader(['en'], gpu=False)

print(type(license_plate_crop)) # <class 'numpy.ndarray'>
print(license_plate_crop)
detections = reader.readtext(license_plate_crop)

初始报错信息

line 619, in get_image_list
    crop_img,ratio = compute_ratio_and_resize(crop_img,width,height,model_height)
  File "/home/pranith_dev/Desktop/Datavoice/Automatic-License-Plate-Recognition-using-YOLOv8/dev-venv/lib/python3.10/site-packages/easyocr/utils.py", line 582, in compute_ratio_and_resize
    img = cv2.resize(img,(int(model_height*ratio),model_height),interpolation=Image.ANTIALIAS)
AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'

降级Pillow后的异常

将Pillow从10.0.0降级到9.5或9.4版本后,问题未解决,输出如下:

<class 'numpy.ndarray'>
[[255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]
 ...
 [255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]]
Illegal instruction

解决方法

1. 修复ANTIALIAS属性报错

Pillow 10.0.0及以上版本已将ANTIALIAS重命名为LANCZOS,而旧版easyocr未适配该变更。可通过以下两种方式解决:

  • 修改easyocr源码:找到报错的utils.py文件(路径如报错信息所示),将第582行的Image.ANTIALIAS替换为Image.LANCZOS,保存后重新运行。
  • 升级easyocr版本:安装兼容Pillow 10+的easyocr版本,执行命令:
    pip install easyocr==1.7.0
    
    该版本已修复Pillow属性名变更的问题,无需修改源码。

2. 解决Illegal instruction错误

该错误通常是由于CPU不支持easyocr预训练模型使用的指令集,或numpy数组格式异常导致:

  • 转换图像格式:将numpy数组先转为PIL图像再传入readtext,避免格式兼容问题:
    from PIL import Image
    
    # 转换numpy数组为PIL图像
    plate_image = Image.fromarray(license_plate_crop)
    detections = reader.readtext(plate_image)
    
  • 更换easyocr版本:尝试安装对老CPU兼容性更好的版本,比如easyocr==1.6.2。
  • 检查CPU兼容性:确保CPU支持AVX2指令集(若使用较老CPU,可尝试编译兼容低指令集的依赖包)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 08:40:32