Why am I getting the error: "Not a JPEG file: starts with 0x89 0x50"

ImagePngJpegCorruption

Image Problem Overview


Why am I getting the message "Not a JPEG file: starts with 0x89 0x50" when I try to open my jpg file?

Image Solutions


Solution 1 - Image

The file is actually a PNG with the wrong file extension. "0x89 0x50" is how a PNG file starts.

Solution 2 - Image

Your file is not a JPEG file, it's just been renamed from a PNG to a JPEG somewhere along the way. Some programs will open this as a recognised file extension and infer the type from the prefix, but obviously not the one you're using.

Solution 3 - Image

Your 'JPEG' file has wrong filename extension 'jpg' or 'jpeg', it's real type is most probably a PNG file.

Just try to rename file name from 'xxx.jpg' or 'xxx.jpeg' to 'xxx.png'.

Under most circumstances, programs distinguish file type with filename extension for convenience, however, if we specify a wrong filename extension(like 'jpg') to a file in other format(like a PNG file), the program will still try to load the PNG file with JPG library, an error will certainly be thrown to user.

Actually, different type of files always have different file header (first 1024 byte)

Here's a quick pass to check real type of the file on Unix-like platform:

using the "file" command, like:

file e3f8794a5c226d4.jpg 

and output is

e3f8794a5c226d4.jpg: PNG image data, 3768 x 2640, 8-bit/color RGBA, non-interlaced

which will print file information details, and we can also check if the specified file has been destructed.

Solution 4 - Image

simply rename *.jpg to *.png. Or open this file in browser

Solution 5 - Image

This is the error response when you try to open a PNG file using a JPEG file viewer which uses libjpeg to open jpeg files. Your file is renamed from png to JPEG as mentioned in earlier answers.

Solution 6 - Image

Here is a python script to identify those fault jpg images in a directory.

import glob 
import os 
import re 
import logging 
import traceback

filelist=glob.glob("/path/to/*.jpg")
for file_obj in filelist:
  try:
	
		jpg_str=os.popen("file \""+str(file_obj)+"\"").read()
		if (re.search('PNG image data', jpg_str, re.IGNORECASE)) or (re.search('Png patch', jpg_str, re.IGNORECASE)):
			print("Deleting jpg as it contains png encoding - "+str(file_obj))
			os.system("rm \""+str(file_obj)+"\"")
  except Exception as e:
	logging.error(traceback.format_exc())
print("Cleaning jps done")

Solution 7 - Image

Here's a modified version of Mohit's script. Instead of deleting misnamed files, it non-destructively renames them.

It also swaps out the os.system() calls for subprocess calls which solves escaping issues regarding quotes in filenames.

import glob
import subprocess
import os
import re
import logging
import traceback

filelist=glob.glob("/path/to/*.jpg")
for file_obj in filelist:
    try:
        jpg_str = subprocess.check_output(['file', file_obj]).decode()
        if (re.search('PNG image data', jpg_str, re.IGNORECASE)) or (re.search('Png patch', jpg_str, re.IGNORECASE)): 

            old_path = os.path.splitext(file_obj)
            if not os.path.isfile(old_path[0]+'.png'):
                new_file = old_path[0]+'.png'
            elif not os.path.isfile(file_obj+'.png'):
                new_file = file_obj+'.png'
            else:
                print("Found PNG hiding as JPEG but couldn't rename:", file_obj)
                continue

            print("Found PNG hiding as JPEG, renaming:", file_obj, '->', new_file)
            subprocess.run(['mv', file_obj, new_file])

    except Exception as e:
        logging.error(traceback.format_exc()) 

print("Cleaning JPEGs done")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmatt burnsView Question on Stackoverflow
Solution 1 - Imagematt burnsView Answer on Stackoverflow
Solution 2 - ImageDavid MView Answer on Stackoverflow
Solution 3 - ImageLiu HaoView Answer on Stackoverflow
Solution 4 - Imageuser9015721View Answer on Stackoverflow
Solution 5 - ImagesaurabheightsView Answer on Stackoverflow
Solution 6 - ImageMohit Arvind khakhariaView Answer on Stackoverflow
Solution 7 - ImageDifferent55View Answer on Stackoverflow