I`m talking about errors like "Corrupt JPEG data: bad Huffman code" or "Corrupt JPEG data: premature end of data segment". Currently, those count as some kind of warnings, not as errors and printed to stderr. But I actually need to catch those errors. Some guy on stackoverflow provided a solution by changing the source code of OpenCV, but I can't figure it out how to make it with Python. He even created a pull request on github, but it was declined.
What I actually need is just to check jpg, jpeg, png files for integrity and corruption, which imread() provides, but I can't extract any useful data from the cv2 just printing it to stderr.
My current code:
from os import listdir
import cv2
import os
dir = './input/'
dest = './output/'
if not os.path.exists(dest):
os.makedirs(dest)
print('Output directory created')
for filename in listdir(dir):
try:
print(filename)
img = cv2.imread(dir+filename)
except cv2.error as e: print('Error: ' + str(e))
It is just prints the filename and if any integrity or corruption happens, cv2 prints it to stderr. I also tried to just redirect the output to another file like this:
python main.py >> output.txt
And then somehow parse it, but I can only see my print, not that one from imread().
So, is there any solution to this problem? Because I tried a lot of corruption checkers, like Pillow verify() method or integv library. But only this one can properly see all my corrupted files.