Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed

在Windows上使用Python PIL库开发博客验证码功能时,遇到The _imagingft C module is not installed错误,需改用Pillow库。安装Pillow后,保存图片时又碰到KeyError: 'jpg',通过将import Image改为from PIL import Image解决了问题。分享中文验证码生成代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在用Python开发自己的博客,需要用到Python生成验证码,当然肯定要用到Python的图形处理库PIL,因为我用的是windows。

所以在安装好pil之后就开始写,就按照题目所说出现了The _imagingft C module is not installed 错误,找了很多建议,最后确定在windows下应该用pillpw。下载地址点击打开链接找到Pillow‑2.5.2.win32‑py2.7.exe因为我用的是python2.7和win32系统,所以就应该下载这个,大家可以根据自己的需求找到响应的包进行安装,Pillow, 它是对PIL的一些BUG修正后的编译版  


到了这里又遇见一个错误,那就是在使用Image.save('code.jpg','jpg')的时候会

(base) czh@czh-virtual-machine:~/下载/YOLOv8hat-helmet40$ pip show pillow Name: pillow Version: 11.1.0 Summary: Python Imaging Library (Fork) Home-page: https://python-pillow.github.io Author: Author-email: "Jeffrey A. Clark" <aclark@aclark.net> License: MIT-CMU Location: /home/czh/anaconda3/lib/python3.13/site-packages Requires: Required-by: bokeh, imageio, matplotlib, scikit-image, streamlit, torchvision, ultralytics (base) czh@czh-virtual-machine:~/下载/YOLOv8hat-helmet40$ pip install Pillow Requirement already satisfied: Pillow in /home/czh/anaconda3/lib/python3.13/site-packages (11.1.0) (base) czh@czh-virtual-machine:~/下载/YOLOv8hat-helmet40$ pip uninstall Pillow Found existing installation: pillow 11.1.0 Uninstalling pillow-11.1.0: Would remove: /home/czh/anaconda3/lib/python3.13/site-packages/PIL/* /home/czh/anaconda3/lib/python3.13/site-packages/pillow-11.1.0.dist-info/* Proceed (Y/n)? y Successfully uninstalled pillow-11.1.0 (base) czh@czh-virtual-machine:~/下载/YOLOv8hat-helmet40$ pip install --upgrade Pillow Collecting Pillow Using cached pillow-11.3.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.metadata (9.0 kB) Using cached pillow-11.3.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (6.6 MB) Installing collected packages: Pillow Successfully installed Pillow-11.3.0 (base) czh@czh-virtual-machine:~/下载/YOLOv8hat-helmet40$ pip install Pillow==8.4.0 Collecting Pillow==8.4.0 Downloading Pillow-8.4.0.tar.gz (49.4 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 49.4/49.4 MB 1.9 MB/s eta 0:00:00 Preparing metadata (setup.py) ... error error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [18 lines of output] Traceback (most recent call last): File "<string>", line 2, in <module> exec(compile(''' ~~~~^^^^^^^^^^^^ # This is <pip-setuptools-caller> -- a caller that pip uses to run setup.py ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<32 lines>... exec(compile(setup_py_code, filename, "exec")) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ''' % ('/tmp/pip-install-5aoxsylq/pillow_429ee359ee4341cd8f966b2e64b4043e/setup.py',), "<pip-setuptools-caller>", "exec")) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<pip-setuptools-caller>", line 35, in <module> File "/tmp/pip-install-5aoxsylq/pillow_429ee359ee4341cd8f966b2e64b4043e/setup.py", line 30, in <module> PILLOW_VERSION = get_version() File "/tmp/pip-install-5aoxsylq/pillow_429ee359ee4341cd8f966b2e64b4043e/setup.py", line 26, in get_version return locals()["__version__"] ~~~~~~~~^^^^^^^^^^^^^^^ KeyError: '__version__' [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
07-04
WARNING:tensorflow:From /root/Python/conda_lit/kind/lib/python3.6/site-packages/tensorflow/python/compat/v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version. Instructions for updating: non-resource variables are not supported in the long term 2025-07-26 19:44:10.231806: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1 2025-07-26 19:44:10.313436: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties: pciBusID: 0000:39:00.0 name: NVIDIA GeForce RTX 4090 computeCapability: 8.9 coreClock: 2.52GHz coreCount: 128 deviceMemorySize: 23.55GiB deviceMemoryBandwidth: 938.86GiB/s 2025-07-26 19:44:10.313630: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory 2025-07-26 19:44:10.313667: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcublas.so.10'; dlerror: libcublas.so.10: cannot open shared object file: No such file or directory 2025-07-26 19:44:10.314718: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10 2025-07-26 19:44:10.314970: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10 2025-07-26 19:44:10.315017: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcusolver.so.10'; dlerror: libcusolver.so.10: cannot open shared object file: No such file or directory 2025-07-26 19:44:10.315049: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcusparse.so.10'; dlerror: libcusparse.so.10: cannot open shared object file: No such file or directory 2025-07-26 19:44:10.315080: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcudnn.so.7'; dlerror: libcudnn.so.7: cannot open shared object file: No such file or directory 2025-07-26 19:44:10.315086: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1598] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform. Skipping registering GPU devices... 2025-07-26 19:44:10.315433: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 AVX512F FMA 2025-07-26 19:44:10.342984: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 2000000000 Hz 2025-07-26 19:44:10.349020: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7f67d0000b60 initialized for platform Host (this does not guarantee that XLA will be used). Devices: 2025-07-26 19:44:10.349043: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version 2025-07-26 19:44:10.350861: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix: 2025-07-26 19:44:10.350875: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] WARNING:tensorflow:From /root/Python/conda_lit/kind/lib/python3.6/site-packages/tf_slim/layers/layers.py:1089: Layer.apply (from tensorflow.python.keras.engine.base_layer_v1) is deprecated and will be removed in a future version. Instructions for updating: Please use `layer.__call__` method instead. loaded ./checkpoint/decom_net_train/model.ckpt loaded ./checkpoint/illumination_adjust_net_train/model.ckpt No restoration pre model! (480, 640, 3) (680, 720, 3) (415, 370, 3) Traceback (most recent call last): File "evaluate.py", line 73, in <module> eval_low_im = load_images(eval_low_data_name[idx]) File "/root/Python/KinD-master/KinD-master/utils.py", line 63, in load_images im = Image.open(file) File "/root/Python/conda_lit/kind/lib/python3.6/site-packages/PIL/Image.py", line 2975, in open fp = builtins.open(filename, "rb") IsADirectoryError: [Errno 21] Is a directory: './test/results'
最新发布
07-27
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值