推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
aoscici2000
V2EX  ›  Python

PIL 处理 base64 图片 老报 OSError 怎么解决?

  •  
  •   aoscici2000 · Dec 11, 2018 · 2916 views
    This topic created in 2729 days ago, the information mentioned may be changed or developed.

    如题, 对 base64 不太熟, 找了很多网上例子(也基本一样的操作), 但老报错,也没找到有效解决方法, 有人能告知一下吗?

    import base64
    from io import BytesIO
    from PIL import Image
    
    base64_data = 'data:image/jpeg;base64,...............'
    byte_data = base64.b64decode(base64_data)
    image_data = BytesIO(byte_data)
    img = Image.open(image_data)
    

    然后到这里就报错了: OSError: cannot identify image file <_io.BytesIO object at 0x000002130267D300>

    用 py 自带的 open 倒是可以直接写入

    2 replies    2018-12-13 10:00:55 +08:00
    Kylin30
        1
    Kylin30  
       Dec 11, 2018
    你试试去掉'data:image/jpeg;base64'前缀,解码后面的 base64 数据。
    aoscici2000
        2
    aoscici2000  
    OP
       Dec 13, 2018
    @Kylin30 果然, 多谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3017 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 14:55 · PVG 22:55 · LAX 07:55 · JFK 10:55
    ♥ Do have faith in what you're doing.