推荐学习书目
› 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
Shingekinoshinji
V2EX  ›  Python

Python PIL 读取报 UserWarning 如何定位

  •  
  •   Shingekinoshinji · Apr 18, 2019 · 6469 views
    This topic created in 2721 days ago, the information mentioned may be changed or developed.

    我在使用 PIL 包读取 JPEG 图片,有时会报如下 UserWarning:

    UserWarning: Possibly corrupt EXIF data.  Expecting to read 8 bytes but only got 0. Skipping tag 41730
      " Skipping tag %s" % (size, len(data), tag))
    
    UserWarning: Corrupt EXIF data.  Expecting to read 12 bytes but only got 6.
      warnings.warn(str(msg))
    

    我想定位出现该问题的图片,所用代码如下:

    from PIL import Image
    def path2image(x):
        try:
            result = Image.open(x).convert("RGB")
            return result
        except:
            print(x)
    

    可是这样并不会运行 except 部分,求问要怎么做

    3 replies  •  2019-04-18 19:10:08 +08:00
    epicnoob
        1
    epicnoob  
       Apr 18, 2019   ❤️ 1
    waring 默认不会抛出错误,用下列代码替换默认行为。

    import warning
    warning.filterwarnings("error", category=UserWarning)
    Shingekinoshinji
        2
    Shingekinoshinji  
    OP
       Apr 18, 2019
    @epicnoob 这是 python2 的方法吗?我是用 python3,好像自带的没有这个包,pip 也没找到
    Shingekinoshinji
        3
    Shingekinoshinji  
    OP
       Apr 18, 2019
    @epicnoob python3 这个包变成了 warnings,解决了,谢谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3006 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 10:56 · PVG 18:56 · LAX 03:56 · JFK 06:56
    ♥ Do have faith in what you're doing.