title: Working with truncated images with PIL
body: <p>I am trying to get the Python 2.7 PIL Library to work with JPEG images that are only available as a stream coming from a HDD image and are not complete. </p>
  
  <p>I have set the option:</p>
  
  <pre><code>ImageFile.LOAD_TRUNCATED_IMAGES = True
  </code></pre>
  
  <p>And load the stream as far as it is available (or better said: as far as I am 100% sure that this data is still a image, not some other file type). I have tested different things and as far as I can tell (for JPEGs) PIL only accepts it as a valid JPEG Image if it finds the <code>0xFFDA</code> (Start of Scan Marker). This is a short example of how I load the data:</p>
  
  <pre><code>from PIL import Image
  from StringIO import StringIO
  
  ImageFile.LOAD_TRUNCATED_IMAGES = True
  
  with open("/path/to/image.raw", 'rb') as fp:
      fp.seek("""jump to position in image where JPEG starts""")
      data = fp.read("""number of bytes I know that those belong to that jpeg""")
      img = Image.open(StringIO(data)) # This would throw exception if the data does 
                                       # not contain the 0xffda marker
      pixel = img.load()               # Would throw exception if LOAD_TRUNCATED_IMAGES = false
  
      height,width = img.size
      for i in range(height):
          for j in range(width):
              print pixel[i,j]
  </code></pre>
  
  <p>On the very last line I expected (or hoped) to see at least the read pixel data to be displayed. But for every pixel it returns <code>(0,0,0)</code>.</p>
  
  <p><strong>The Question: Is what I am trying here not possible with PIL?</strong></p>
  
  <p>Some weeks ago I tried the same with a image file I truncated myself, simply by cutting data from it with an editor. It worked for the pixel-data that was available. As soon as it reached a pixel that I cut off, the program threw an exception (I will try this again later today to make sure that I am not remembering wrong). </p>
  
  <p>If somebody is wondering why I am doing this: I need to make sure that the image/picture inside that hdd image is in consecutive blocks/clusters and is not fragmented. To make sure of this I wanted to use pixel matching. </p>
  
  <p><strong>EDIT:</strong>
  I have tried it again and this is what I have seen.</p>
  
  <ul>
  <li><p>I opened a truncated image in GIMP and it showed me a few pixel lines in the upper part, but PIL was not able to at least give me the RGB values of those pixels. It always returns (0,0,0).</p></li>
  <li><p>I made the image slightly bigger such that the lower 4/5 of the image was not visible, but that was enough for PIL to show me the RGB values that were available. Everything else was (0,0,0).</p></li>
  </ul>
  
  <p>I am still not 100% sure whether PIL can show me the RGB values, even if only view pixel-data is available. </p>