我正在使用OpenCV从文件夹中读取图像。会出现很多这样的消息:
Corrupt JPEG data: premature end of data segment
Premature end of JPEG file
Premature end of JPEG file
Premature end of JPEG file
如何捕获该异常并删除这些图像文件?
我正在使用OpenCV从文件夹中读取图像。会出现很多这样的消息:
Corrupt JPEG data: premature end of data segment
Premature end of JPEG file
Premature end of JPEG file
Premature end of JPEG file
如果您说您正在阅读“图像”(多个图像),则需要循环遍历从中读取它们的文件夹中的文件。 在这种情况下,如果您使用以下方法检查图像是否有效或无效:
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;
}
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()