How to detect if the jpg jpeg image file is corrupted(incomplete)?

19,147

Solution 1

If you need a "programmatic" approach rather than the command line approach suggested by @MarkSetchell, you could create a very quick test for this in pretty much any programming language. Note that this will only find the kind of truncating corruption you mention in your question. Mark's method may be more reliable for finding corruption in general.

As we know, any JPEG file or stream is written according to the JPEG Interchange Format. That means that they must start with a SOI (Start-of-Image) marker (the two bytes 0xFF, 0xD8) and end with the EOI (End-of-Image) marker (two bytes, 0xFF, 0xD9). These two markers will not be found anywhere else in a JPEG file/stream.

If you first identify the file as a JPEG by inspecting the first two bytes and matching against the SOI marker, you could skip to the end and search backwards for the EOI marker. Most likely, this will either be the last two bytes or you will not find them at all. But it may be safer to do a search (perhaps for a limited length), as I think it may be allowed to place application-specific data in a JPEG file after the EOI (someone, please correct me if I'm wrong).

Solution 2

I created a JPEG to test this using ImageMagick as follows:

convert -size 1024x768 gradient: image.jpg

and it was 14kB. Your image looks like it is incomplete, so I chopped off everything after 3kB like this:

dd if=image.jpg bs=3000 count=1 > corrupt.jpg

Now, if I run ImageMagick's identify command and discard stdout, just retaining stderr, I get:

identify -verbose corrupt.jpg > /dev/null

Sample Output

identify: Premature end of JPEG file `corrupt.jpg' @ warning/jpeg.c/JPEGWarningHandler/364.
identify: Corrupt JPEG data: premature end of data segment `corrupt.jpg' @ warning/jpeg.c/JPEGWarningHandler/364.

Alternatively, you could discard stderr too and simply look at the exit code (0=success, anything else=error):

identify -regard-warnings -verbose corrupt.jpg > /dev/null 2>&1
echo $?
1

whereas for a complete image:

identify -regard-warnings -verbose image.jpg > /dev/null 2>&1
echo $?
0

ImageMagick is installed on most Linux distros and is available for macOS/OSX and Windows.

Share:
19,147
JasonHsieh
Author by

JasonHsieh

Updated on June 05, 2022

Comments

  • JasonHsieh
    JasonHsieh about 2 years

    I have to show some images from others' image server on my website but some of the images from the image server can only partially show like below image enter image description here

    The image included width and height info but only show very top part of image. If I open the image with Chrome v61, it looks like below image enter image description here

    Chrome v61 shows this color to present the transparency in png image file but what does it mean in jpg jpeg image file?

    Is there anyone knows how to detect this kind of corrupted(incomplete) image? I'm trying to aviod this kind of images showing on my website.

  • JasonHsieh
    JasonHsieh over 6 years
    Thanks mate.Is there any way I can identify the corrupted jpg on browser JavaScript ?
  • Harald K
    Harald K over 6 years
    @JasonHsieh Sure, if you can access the compressed image data, just do what I outlined above. If you need help writing that code, I'm not the one. Ask a new question specific for the task, and make sure you include what you have tried so far.
  • user3344003
    user3344003 over 6 years
    The JPEG stream is over once the EOI marker is reached. In theory data can go afterwards but it is unpredictable how an encoder might stream the stream.
  • schoetbi
    schoetbi almost 4 years
    With find . | xargs -I % sh -c 'identify -regard-warnings -verbose % > /dev/null 2>&1;echo % $?' you get the list of filenames as the first column and the result as a second column
  • Brian Di Palma
    Brian Di Palma over 3 years
    A small modification to @schoetbi answer allows you to move the corrupt files out of a directory find . | xargs -I % sh -c 'identify -verbose % > /dev/null 2>&1; if [ $? -eq 1 ]; then mv % ../unique-organized-corrupt; fi '