Python PIL 读取报 UserWarning 如何定位

2019-04-18 16:19:50 +08:00
 Shingekinoshinji

我在使用 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 部分,求问要怎么做

5261 次点击
所在节点    Python
3 条回复
epicnoob
2019-04-18 16:33:16 +08:00
waring 默认不会抛出错误,用下列代码替换默认行为。

import warning
warning.filterwarnings("error", category=UserWarning)
Shingekinoshinji
2019-04-18 19:05:02 +08:00
@epicnoob 这是 python2 的方法吗?我是用 python3,好像自带的没有这个包,pip 也没找到
Shingekinoshinji
2019-04-18 19:10:08 +08:00
@epicnoob python3 这个包变成了 warnings,解决了,谢谢!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/556465

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX