Working with truncated images with PIL

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.

I have set the option:

ImageFile.LOAD_TRUNCATED_IMAGES = True

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 0xFFDA (Start of Scan Marker). This is a short example of how I load the data:

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]

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 (0,0,0) .

The Question: Is what I am trying here not possible with PIL?

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).

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.

EDIT: I have tried it again and this is what I have seen.

  • 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).

  • 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).

  • I am still not 100% sure whether PIL can show me the RGB values, even if only view pixel-data is available.


    I would try it with an uncompressed format like TGA. JPG being a compressed format may not make any sense to extract pixels from an incomplete image. JPEG actually stores the parameters for equations that describe the image, not pixel values. When you query a JPEG for a pixel value it evaluates the equations at that point and returns the result.


    I don't really know about streaming, but I think that you simply cannot access rgb value the way you do. Try:

    rgb_im = img.convert('RGB')
    r, g, b = rgb_im.getpixel((i, j))
    
    链接地址: http://www.djcxy.com/p/50050.html

    上一篇: 如何根据已知区域锚点在两个图像之间投影像素

    下一篇: 用PIL处理截断图像