title: Repairing corrupted jpegs
body: <p>Time ago my external HDD crashed (stopped working completely) and I had to recover the files from another HDD from which they had been erased. Some time later I realized that some of the pictures that had been recovered were corrupted.</p>
  
  <p>I couldnt open those images by means of any image program. When I run the "file" command over the .jpgs I get:</p>
  
  <pre><code>DSCN9129.JPG: JPEG image data, EXIF standard 2.2
  DSCN9130.JPG: JPEG image data, EXIF standard 2.2
  DSCN9131.JPG: JPEG image data, EXIF standard 2.2
  DSCN9132.JPG: DOS executable (COM)
  DSCN9133.JPG: data
  DSCN9134.JPG: data
  DSCN9135.JPG: JPEG image data, EXIF standard 2.2
  DSCN9136.JPG: data
  DSCN9137.JPG: data
  DSCN9138.JPG: data
  DSCN9139.JPG: data
  DSCN9140.JPG: 8086 relocatable (Microsoft)
  DSCN9141.JPG: data
  DSCN9142.JPG: data
  </code></pre>
  
  <p>There, we can notice that some images are ok, some are not recognized and some others are wrongly interpreted as other kind of files.</p>
  
  <p>I am uploading 2 example images:</p>
  
  <p><a href="http://ul.to/u7emvxp8" rel="nofollow noreferrer">http://ul.to/u7emvxp8</a>   ---> DSCN9133.JPG   (corrupted)<br>
  <a href="http://ul.to/zegf7q8a" rel="nofollow noreferrer">http://ul.to/zegf7q8a</a>   ---> DSCN9131.JPG   (not corrupted)</p>
  
  <p>I used this python code in order to see what's in the file:</p>
  
  <pre><code>import sys, os
  
  with open("./images/DSCN9133.JPG", "rb") as f:
          stri=""
          i=0
          byte = f.read(1)
          while byte != "" and i &lt; 1000:
                  i=i+1
                  stri = stri+" "+hex(ord(byte))
                  byte = f.read(1)
          print(stri)
  </code></pre>
  
  <p>I'm printing here the first few bytes of a corrupted and a not corrupted image.</p>
  
  <p>CORRUPTED:</p>
  
  <pre><code>0x6b 0xe8 0xf2 0xd6 0x9b 0xba 0x77 0xb4 0x97 0xd1 0x69 0xbb 0x2e 0xe 0xda 0x8d 0x4 0xb7 0x7 0x33 0xb2 0xf2 0x10 0x35 0x58 0x4e 0xa1 0x80 0x4c 0x2b 0x1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 0x0 0x1 0x0 0x0 0x0 0x1 0x0 0x0 0x0 0x1a 0x1 0x5 0x0 0x1 0x0 0x0 0x0 0xd8 0x0 0x0 0x0 0x1b 0x1 0x5 0x0 0x1 0x0 0x0 0x0 0xe0 0x0 0x0 0x0 0x28 0x1 0x3 0x0 0x1 0x0 0x0 0x0 0x2 0x0 0x0 0x0 0x31 0x1 0x2 0x0 0xa 0x0 0x0 0x0 0xe8 0x0 0x0 0x0 0x32 0x1 0x2 0x0 0x14 0x0 0x0 0x0 0x8 0x1 0x0 0x0 0x13 0x2 0x3 0x0 0x1 0x0 0x0 0x0 0x2 0x0 0x0 0x0 0x69 0x87 0x4 0x0 0x1 0x0 0x0 0x0 0x1c 0x1 0x0 0x0 0xa4 0x3 0x0 0x0 0x20 0x20 0x20 0x20 0x20 0x20 0x20 0x20 0x20 0x20 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x4e 0x49 0x4b 0x4f 0x4e 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x45 0x33 0x37 0x30 0x30 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x2c 0x1 0x0 0x0 0x1 0x0 0x0 0x0 0x2c 
  </code></pre>
  
  <p>but I don't know what jpgs are made of...</p>
  
  <p>Can someone give me a piece of advice the direction I should follow in order to find out whether it is possible to repair the images or not?</p>
  
  <p>How can I tell if the pixel information is there or I've just been keeping random data?</p>
  
  <p>I think that knowing the size of the images is a good thing, but of course since these images are compressed it won't be that easy.</p>
  
  <p>EDIT: I found this <a href="http://www.w3.org/Graphics/JPEG/itu-t81.pdf" rel="nofollow noreferrer">http://www.w3.org/Graphics/JPEG/itu-t81.pdf</a> that should be useful for this, but I also would like to know how can I tell if the pixel data is there or not. </p>
  
  <p>EDIT2: My original question was focused on the following point (I clarify because I realized it wasn't clear enough) I think that it is possible that the bytes on those images are part of jpegs. It can be that the bytes are just shifted or the header is missing, and maybe there is a way to explore whether the data is there. Of course if these were BMPs or uncompressed images of some king this could be the case. Being jpgs, that are compressed I think makes everything way more difficult of not completely impossible. But still, I wonder more in general, if i told someone that among certain bunch of bytes there are the pixel information from an image, how could we proceed?</p>