解决EasyOCR运行时PIL.Image无ANTIALIAS属性的AttributeError问题
解决EasyOCR中
AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'错误 问题背景
使用EasyOCR识别程序截图文本,已通过pip install easyocr完成安装,执行识别代码时触发AttributeError,卸载重装EasyOCR后问题依旧。
代码示例
def text_recognition(im, languages): reader = easyocr.Reader(languages) language = reader.readtextlang(im, paragraph=True) if language == 'en': result = reader.readtext(im, paragraph=True) print(result) return result text = text_recognition('screenshot1.png', ['en', 'ru'])
错误信息
Traceback (most recent call last): File "C:\Users\childoflogos\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner self.run() File "C:\Users\childoflogos\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 975, in run self._target(*self._args, **self._kwargs) File "C:\Users\childoflogos\Desktop\auto_standoff_software\main3.py", line 225, in parse text = text_recognition('screenshot1.png', ['en', 'ru']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\childoflogos\Desktop\auto_standoff_software\main3.py", line 16, in text_recognition language = reader.readtextlang(im, paragraph=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\childoflogos\Desktop\auto_standoff_software\venv\Lib\site-packages\easyocr\easyocr.py", line 500, in readtextlang result = self.recognize(img_cv_grey, horizontal_list, free_list,\ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\childoflogos\Desktop\auto_standoff_software\venv\Lib\site-packages\easyocr\easyocr.py", line 383, in recognize image_list, max_width = get_image_list(h_list, f_list, img_cv_grey, model_height = imgH) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\childoflogos\Desktop\auto_standoff_software\venv\Lib\site-packages\easyocr\utils.py", line 613, in get_image_list crop_img,ratio = compute_ratio_and_resize(crop_img,width,height,model_height) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\childoflogos\Desktop\auto_standoff_software\venv\Lib\site-packages\easyocr\utils.py", line 576, 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'
已尝试操作
pip uninstall easyocr # 输入y确认卸载 pip install easyocr
错误原因
PIL(Pillow)在10.0.0版本中,将Image.ANTIALIAS重命名为Image.LANCZOS,而当前使用的EasyOCR版本未适配该变更,导致代码调用旧常量时触发属性不存在错误。
解决方案
方案1:降级Pillow到兼容版本
执行以下命令,将Pillow安装到10.0.0以下的稳定版本:
pip install pillow<10.0.0
方案2:修改EasyOCR源码临时修复
找到报错路径中的utils.py文件(示例路径:venv\Lib\site-packages\easyocr\utils.py),定位到第576行,将Image.ANTIALIAS替换为Image.LANCZOS,保存后重新运行代码。
方案3:升级EasyOCR到最新版本
部分新版本EasyOCR已修复该兼容问题,执行升级命令:
pip install --upgrade easyocr
内容的提问来源于stack exchange,提问作者tegularis
相关产品推荐
相关产品推荐

