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

求助:使用Keras构建CNN时遇StopIteration错误,PIL导入失败

Fixing "StopIteration: Could not import PIL.Image. The use of array_to_img requires PIL" in Keras

Hey there! I totally get how frustrating this error can be when you're just starting out with CNNs and Keras—especially after already trying to reinstall Pillow. Let's walk through some targeted fixes that often resolve this issue:

  • Double-check your Python environment
    It's super common to have multiple Python environments (like system Python, conda envs, or virtualenvs) running on your machine, and the Pillow you reinstalled might not be in the same environment Keras is using.

    • Run which python (Linux/macOS) or where python (Windows) to see the path of the Python you're using for your Keras project.
    • Run pip --version to confirm that pip is linked to that same Python path.
    • If you're using conda, make sure you've activated your project environment first: conda activate your_env_name, then install Pillow with either conda install pillow or pip install pillow.
  • Do a clean, cache-free reinstall of Pillow
    Sometimes cached installation files get corrupted. Try wiping the cache and installing a fresh copy:

    pip uninstall -y pillow
    pip install --no-cache-dir pillow
    
  • Test PIL import outside of Keras
    Let's isolate the problem first. Open a plain Python terminal and run:

    from PIL import Image
    

    If this throws an error, the issue is with Pillow's installation itself, not Keras. If it works, then the problem might be in how Keras is trying to import it, or there's a naming conflict in your project (like a file named PIL.py that's overriding the real package).

  • Check version compatibility between Keras and Pillow
    Occasional version mismatches can cause import issues. Try installing a specific, well-compatible version of Pillow. For most modern Keras (2.x+), Pillow 9.x tends to work well:

    pip install pillow==9.5.0
    
  • Fix Jupyter Notebook environment mismatches
    If you're running code in Jupyter, the notebook kernel might be using a different Python environment than your terminal. In your notebook, run this to find the kernel's Python path:

    import sys
    print(sys.executable)
    

    Then install Pillow directly into that environment with:

    !{sys.executable} -m pip install pillow
    
  • Explicitly import array_to_img in your code
    Add this line at the top of your script/notebook before using array_to_img:

    from keras.preprocessing.image import array_to_img
    

    This can sometimes bypass any indirect import issues Keras might be having.

Give these steps a try in order—one of them should get you past this error. If you hit a different message or something doesn't work, feel free to share more details!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:08:58