如何以编程方式检查图像(PNG,JPEG或GIF)是否已损坏? [英] How do I programmatically check whether an image (PNG, JPEG, or GIF) is corrupted?

查看:129
本文介绍了如何以编程方式检查图像(PNG,JPEG或GIF)是否已损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好。所以我有大约250,000张高分辨率图像。我想要做的就是浏览所有这些并找到损坏的。如果您知道4scrape是什么,那么您就知道图像的本质了。

Okay. So I have about 250,000 high resolution images. What I want to do is go through all of them and find ones that are corrupted. If you know what 4scrape is, then you know the nature of the images I.

对我来说,损坏的是图像被加载到Firefox中它说

Corrupted, to me, is the image is loaded into Firefox and it says


无法显示图像此类图像,因为它包含错误。

现在,我可以选择所有250,000张图像(~150gb)并将它们拖放到Firefox中。那会很糟糕,因为我不认为Mozilla设计Firefox可以打开250,000个标签。不,我需要一种方法来以编程方式检查图像是否已损坏。

Now, I could select all of my 250,000 images (~150gb) and drag-n-drop them into Firefox. That would be bad though, because I don't think Mozilla designed Firefox to open 250,000 tabs. No, I need a way to programmatically check whether an image is corrupted.

有没有人知道PHP或Python库可以在这些方面做些什么?或者现有的Windows软件?

Does anyone know a PHP or Python library which can do something along these lines? Or an existing piece of software for Windows?

我已经删除了明显损坏的图像(例如0字节的图像)但是我有99.9%的确定那里在我的人群中漂浮着更多患病的图像。

I have already removed obviously corrupted images (such as ones that are 0 bytes) but I'm about 99.9% sure that there are more diseased images floating around in my throng of a collection.

推荐答案

一种简单的方法是尝试加载和验证文件使用PIL(Python成像库)。

An easy way would be to try loading and verifying the files with PIL (Python Imaging Library).

from PIL import Image

v_image = Image.open(file)
v_image.verify()

抓住例外......

Catch the exceptions...

来自文档

im.verify()

尝试确定文件是否已损坏,实际上没有解码图像数据。如果此方法发现任何问题,则会引发适当的异常。此方法仅适用于新打开的图像;如果图像已加载,则结果未定义。此外,如果您需要在使用此方法后加载图像,则必须重新打开图像文件。

这篇关于如何以编程方式检查图像(PNG,JPEG或GIF)是否已损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆