如何在 c++ 中验证图像文件的完整性还是蟒蛇?
我想检查图像是否已完全下载。有没有什么库可以用? 我想要验证的图像包括jpeg、png、bmp等各种格式。
I want to check whether the images is downloaded completely. Is there any library to use?
The images I want to verify including various formats such jpeg, png, bmp etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Python 中此类事情的标准首选库是 Python 成像库 (PIL)。
The standard go-to library for that kind of thing in Python is the Python Imaging Library (PIL).
我使用了 Pyhton Pillow 模块(PIL)和 Imagemagick 包装棒(用于 psd、xcf 格式)来检测损坏的图像,带有代码片段的原始答案是 这里。
我还在我的 Python 脚本GitHub 上中实现了此解决方案。
我还验证了损坏的文件(jpg)通常不是“损坏的”图像,即损坏的图片文件有时仍然是合法的图片文件,原始图像丢失或更改,但您仍然可以加载它。
为了完整性,我引用完整的答案:
您可以使用 Python Pillow(PIL) 模块(对于大多数图像格式)来检查文件是否是有效且完整的图像文件。
如果您的目标是检测损坏的图像,@Nadia Alramli 正确建议使用
im.verify()
方法,但是此无法检测所有可能的图像缺陷,例如,im.verify
不会检测截断的图像(大多数查看者通常会加载灰色区域)。Pillow 也能够检测这些类型的缺陷,但您必须应用图像处理或图像解码/重新编码或触发检查。最后我建议使用此代码:
如果图像有缺陷,此代码将引发异常。
请考虑到 im.verify 比执行图像操作快大约 100 倍(我认为翻转是更便宜的转换之一)。
使用此代码,您将以大约 10 MB/秒(现代 2.5Ghz x86_64 CPU)的速度验证一组图像。
对于其他格式psd,xcf,..可以使用Imagemagick包装Wand,代码如下:
但是,从我的实验来看,Wand 没有检测到截断的图像,我认为它会在没有提示的情况下将缺少的部分加载为灰色区域。
我红色的是Imagemagick有一个外部命令identify可以完成这项工作,但我还没有找到一种以编程方式调用该函数的方法,我没有测试过这条路线。
我建议始终执行初步检查,检查文件大小不为零(或非常小),这是一个非常便宜的想法:
I have used Pyhton Pillow module (PIL) and Imagemagick wrapper wand (for psd, xcf formats) in order to detect broken images, the original answer with code snippets is here.
I also implemented this solution in my Python script here on GitHub.
I also verified that damaged files (jpg) frequently are not 'broken' images i.e, a damaged picture file sometimes remains a legit picture file, the original image is lost or altered but you are still able to load it.
I quote the full answer for completeness:
You can use Python Pillow(PIL) module, with most image formats, to check if a file is a valid and intact image file.
In the case you aim at detecting also broken images, @Nadia Alramli correctly suggests the
im.verify()
method, but this does not detect all the possible image defects, e.g.,im.verify
does not detect truncated images (that most viewers often load with a greyed area).Pillow is able to detect these type of defects too, but you have to apply image manipulation or image decode/recode in or to trigger the check. Finally I suggest to use this code:
In case of image defects this code will raise an exception.
Please consider that im.verify is about 100 times faster than performing the image manipulation (and I think that flip is one of the cheaper transformations).
With this code you are going to verify a set of images at about 10 MBytes/sec (modern 2.5Ghz x86_64 CPU).
For the other formats psd,xcf,.. you can use Imagemagick wrapper Wand, the code is as follows:
But, from my experiments Wand does not detect truncated images, I think it loads lacking parts as greyed area without prompting.
I red that Imagemagick has an external command identify that could make the job, but I have not found a way to invoke that function programmatically and I have not tested this route.
I suggest to always perform a preliminary check, check the filesize to not be zero (or very small), is a very cheap idea:
您可以通过尝试将图像加载到内存中(使用 PIL 或类似的方法)来猜测,但有些图像可能在不完整的情况下加载正常 - 例如,如果您有标题和第一帧,则动画 GIF 可能会加载良好动画,并且您不会注意到动画的后续帧丢失了。
更可靠的方法可能是使用一些带外通信,例如不要监视文件夹并在新文件存在时立即处理它们,而是找到某种方法来挂钩下载程序进程并让它向您发出信号当它决定它准备好了时。
You can guess by attempting to load the image into memory (using PIL or somesuch), but it's possible that some images could be loaded ok without being complete - for example an animated GIF might load fine if you have the header and the first frame of the animation, and you won't notice that later frames of the animation were missing.
A more reliable approach would probably be to use some out-of-band communication, like rather than watching a folder and processing new files as soon as they exist, find some way of hooking into the downloader process and getting it to give you a signal when it decides it is ready.