[python]解决AttributeError: module ‘PIL.Image‘ has no attribute ‘ANTIALIAS‘问题

环境:python3.13.1

问题:

利用ddddocr进行验证码识别时报AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'错误信息,具体如下:

python3
Python 3.13.1 (main, Jan  6 2025, 22:29:09) [Clang 16.0.0 (clang-1600.0.26.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from ddddocr import DdddOcr
...
... ocr = DdddOcr()
... ocr = DdddOcr(show_ad=False)
...
Traceback (most recent call last):
  File "<python-input-0>", line 4, in <module>
    ocr = DdddOcr(show_ad=False)
TypeError: DdddOcr.__init__() got an unexpected keyword argument 'show_ad'
>>> from ddddocr import DdddOcr
...
... ocr = DdddOcr()
...
>>>
>>> with open('code.png', 'rb') as f:
...     image_bytes = f.read()
...
>>> result = ocr.classification(image_bytes)
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    result = ocr.classification(image_bytes)
  File "/Users/apple/.pyenv/versions/3.13.1/lib/python3.13/site-packages/ddddocr/__init__.py", line 466, in classification
    image = image.resize((int(image.size[0] * (64 / image.size[1])), 64), Image.ANTIALIAS).convert('L')
                                                                          ^^^^^^^^^^^^^^^
AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'
>>>

问题原因:

        pillow的ANTIALIAS属性在高版本python不支持了

解决方案:

        降低python版本和pillow版本(网上答案)

技术验证:

(1)python 3.11.10版本
pip3 list
Package                Version
---------------------- -----------
coloredlogs            15.0.1
ddddocr                1.5.6
easyocr                1.7.2
filelock               3.16.1
flatbuffers            24.12.23
fsspec                 2024.12.0
humanfriendly          10.0
imageio                2.36.1
Jinja2                 3.1.5
lazy_loader            0.4
MarkupSafe             3.0.2
mpmath                 1.3.0
networkx               3.4.2
ninja                  1.11.1.3
numpy                  2.2.1
onnxruntime            1.20.1
opencv-python-headless 4.10.0.84
packaging              24.2
pillow                 11.1.0
pip                    24.3.1
protobuf               5.29.2
pyclipper              1.3.0.post6
python-bidi            0.6.3
PyYAML                 6.0.2
scikit-image           0.25.0
scipy                  1.15.0
setuptools             65.5.0
shapely                2.0.6
sympy                  1.13.3
tifffile               2024.12.12
torch                  2.2.2
torchvision            0.17.2
typing_extensions      4.12.2
python3
Python 3.11.10 (main, Jan  6 2025, 11:15:25) [Clang 16.0.0 (clang-1600.0.26.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from ddddocr import DdddOcr
>>>
>>> ocr = DdddOcr()
欢迎使用ddddocr,本项目专注带动行业内卷,个人博客:wenanzhe.com
训练数据支持来源于:http://146.56.204.113:19199/preview
爬虫框架feapder可快速一键接入,快速开启爬虫之旅:https://github.com/Boris-code/feapder
谷歌reCaptcha验证码 / hCaptcha验证码 / funCaptcha验证码商业级识别接口:https://yescaptcha.com/i/NSwk7i

>>>
>>> with open('code.png', 'rb') as f:
...     image_bytes = f.read()
...
>>> result = ocr.classification(image_bytes)
>>> result
'7ta9'
(2)python 3.12.8版本
#切换python版本
pyenv local 3.12.8

pip3 list

Package                Version
---------------------- ---------
coloredlogs            15.0.1
ddddocr                1.5.6
flatbuffers            24.12.23
humanfriendly          10.0
mpmath                 1.3.0
numpy                  2.2.1
onnxruntime            1.20.1
opencv-python-headless 4.10.0.84
packaging              24.2
pillow                 11.1.0
pip                    24.3.1
protobuf               5.29.2
sympy                  1.13.3
 python3
Python 3.12.8 (main, Jan  6 2025, 15:42:55) [Clang 16.0.0 (clang-1600.0.26.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from ddddocr import DdddOcr
>>>
>>> ocr = DdddOcr()
欢迎使用ddddocr,本项目专注带动行业内卷,个人博客:wenanzhe.com
训练数据支持来源于:http://146.56.204.113:19199/preview
爬虫框架feapder可快速一键接入,快速开启爬虫之旅:https://github.com/Boris-code/feapder
谷歌reCaptcha验证码 / hCaptcha验证码 / funCaptcha验证码商业级识别接口:https://yescaptcha.com/i/NSwk7i
>>>
>>> with open('code.png', 'rb') as f:
...     image_bytes = f.read()
...
>>> result = ocr.classification(image_bytes)
>>> result
'7ta9'
>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农丁丁

你的认可是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值