检测损坏的 png 文件的有效方法?
我编写了一个程序来处理一堆由单独进程生成的 png 文件。捕获大部分工作正常,但有时进程会终止并重新启动,从而留下损坏的图像。我无法检测进程何时终止或哪个文件终止(大约有 3000 个 png 文件)。
有没有好的方法来检查损坏的 png 文件?
I've written a program to process a bunch of png files that are generated by a seperate process. The capture mostly works, however there are times when the process dies and is restarting which leaves a corrupted image. I have no way to detect when the process dies or which file it dies one (there are ~3000 png files).
Is there a good way to check for a corrupted png file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于您使用的是 Linux 系统,因此您可能已经安装了 Python。
一种简单的方法是尝试使用 PIL(Python 成像库)加载和验证文件(您需要先安装它)。
(逐字取自我自己在这个帖子中的回答)
Since you're on a Linux system you probably already have Python installed.
An easy way would be to try loading and verifying the files with PIL (Python Imaging Library) (you'd need to install that first).
(taken verbatim from my own answer in this thread)
我知道这是 2010 年的问题,但我认为这是一个更好的解决方案: pngcheck 。
I know this is a question from 2010, but I think this is a better solution: pngcheck.
另一种可能的解决方案是稍微改变处理器处理文件的方式:让它始终创建一个名为 temp.png 的文件(例如),然后在完成后将其重命名为“正确”的名称。这样,您就知道周围是否有一个名为 temp.png 的文件,那么该过程就会被中断,而如果没有这样的文件,那么一切都很好。
(一种变体命名方案是像 Firefox 的下载程序一样 - 将 .partial 附加到真实文件名以获取临时名称。)
A different possible solution would be to slightly change how your processor processes the files: Have it always create a file named temp.png (for example), and then rename it to the "correct" name once it's done. That way, you know if there is a file named temp.png around, then the process got interrupted, whereas if there is no such file, then everything is good.
(A variant naming scheme would be to do what Firefox's downloader does -- append .partial to the real filename to get the temporary name.)
有点黑客,但有效
如果你在 Linux 或类似的系统上运行,你可能有“转换”命令,
如果你制作了一个无效的 png,然后尝试转换,你会得到一个错误:
Kind of a hack, but works
If you are running on linux or something like you might have the "convert" command
If you make an invalid png, and then try to convert, you'll get an error:
查找所有非 PNG 文件:
查找所有损坏的 PNG 文件:
file
命令仅检查幻数。拥有 PNG 幻数并不意味着它是格式良好的 PNG 文件。magick recognize
是 ImageMagick 的一个工具。默认情况下,它仅检查文件头以获得更好的性能。这里我们使用+ping
禁用该功能并让identify
读取整个文件。Find all non-PNG files:
Find all corrupted PNG files:
file
command only checks magic number. Having the PNG magic number doesn't mean it is a well formed PNG file.magick identify
is a tool from ImageMagick. By default, it only checks headers of the file for better performance. Here we use+ping
to disable the feature and makeidentify
read the whole file.