V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2019-04-18 16:19:50 +08:00 · 5222 次点击
    这是一个创建于 1806 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在使用 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 条回复    2019-04-18 19:10:08 +08:00
    epicnoob
        1
    epicnoob  
       2019-04-18 16:33:16 +08:00   ❤️ 1
    waring 默认不会抛出错误,用下列代码替换默认行为。

    import warning
    warning.filterwarnings("error", category=UserWarning)
    Shingekinoshinji
        2
    Shingekinoshinji  
    OP
       2019-04-18 19:05:02 +08:00
    @epicnoob 这是 python2 的方法吗?我是用 python3,好像自带的没有这个包,pip 也没找到
    Shingekinoshinji
        3
    Shingekinoshinji  
    OP
       2019-04-18 19:10:08 +08:00
    @epicnoob python3 这个包变成了 warnings,解决了,谢谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5672 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 06:29 · PVG 14:29 · LAX 23:29 · JFK 02:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.