我正在使用OpenCV从文件夹中读取图像。出现了很多这样的消息:
Corrupt JPEG data: premature end of data segment
Premature end of JPEG file
Premature end of JPEG file
Premature end of JPEG file
如何捕获此异常并删除这些图像文件?
发布于 2015-05-16 11:33:42
由于您说您正在读取'images‘(多个图像),因此您将遍历从中读取这些文件的文件夹中的文件。在这种情况下,如果您通过以下方式检查镜像是否有效:
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
然后,您可以继续删除损坏/损坏的文件。
发布于 2021-02-20 02:32:24
我也一直在努力寻找解决方案。阅读了几十篇文章,其中大多数只是声明openCV不会抛出错误,并且只在stderr上输出错误。有些人建议使用PIL,但这并不能检测出大多数图像损坏。通常只会过早结束文件。
然而,OpenCV警告的相同错误可以通过imagemagick检测到。
import subprocess
def checkFile(imageFile):
try:
subprocess.run(["identify", "-regard-warnings", imageFile]).check_returncode()
return true
except (subprocess.CalledProcessError) as e:
return false
如果您不希望检查对您的输出造成垃圾邮件,请将stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
参数添加到运行函数调用中。
在windows上,如果尚未安装旧式命令,请使用新语法:
subprocess.run(["magick", "identify", "-regard-warnings", imageFile]).check_returncode()
https://stackoverflow.com/questions/30237613
复制相似问题