Premature end of JPEG file python

  1. Problem description

‘Premature end of JPEG file’ is a warning when using cv2 (python version of opencv ) to read the image, the warning code is located in libjpeg

JMESSAGE(JWRN_JFIF_MAJOR, "Warning: unknown JFIF revision number %d.%02d")
184	JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file")
185	JMESSAGE(JWRN_MUST_RESYNC,
186	         "Corrupt JPEG data: found marker 0x%02x instead of RST%d")

The specific link is https://github.com/opencv/opencv/search?utf8=%E2%9C%93&q=JWRN_JPEG_EOF

When debugging the positioning image, it is indeed found that the image is broken.

Second, the solution

In order to let the python code catch the exception, the program is modified as follows:

try:
    img = Image.open(absolute_path)
except IOError:
    print(absolute_path)
try:
    img= np.array(img, dtype=np.float32)
except :
    print('corrupt img',absolute_path)

absolute_path is the full path (absolute path) of the file, i.e. file path + file name

Leave a Comment

Your email address will not be published. Required fields are marked *