Skip to content

Image plugin fails despite correct tile settings #7536

Closed

Description

@kamocat

What did you do?

I created a working function to read FLIR FFF files.
Then I started a new plugin based off that function, using the handbook instructions.

What did you expect to happen?

I expected the plugin to succeed in opening the image.

What actually happened?

Pillow tried to open it twice, and then failed with a generic error.

PIL.UnidentifiedImageError: cannot identify image file 'Snap-000014.img

What are your OS, Python and Pillow versions?

  • OS: Windows 10
  • Python: Python 3.10.10
  • Pillow: 9.4.0
from PIL import Image, ImageFile, ImageStat

def _accept(prefix):
    return prefix[:3] == b"FFF"

def u16(arr, index):
    return int.from_bytes(arr[index:index+2], 'little')

class FFFImageFile(ImageFile.ImageFile):
    format = "FFF"
    format_description = "Raw image exported by FLIR ResearchIR"
    
    def _open(self):
        h = self.fp.read(2780)
        print(h[:16]) #To verify that this is called
        s = u16(h,0x6c)
        self._size = (
            u16(h,s+2),
            u16(h,s+4),
        )
        self._mode = 'I;16L'
        
        self.tile = [('raw', (0,0) + self.size, s+32, (self.mode, 0, 1))]
        print(self.mode)
        

Image.register_open(FFFImageFile.format, FFFImageFile, _accept)

Image.register_extensions(
    FFFImageFile.format,
    [
        ".img", #Single image
        ".seq", #Series of images
    ],
)

def read(fname):
    d = []
    with open(fname, 'rb') as f:
        d = f.read()
    s = u16(d,0x6c)
    size = (
        u16(d,s+2),
        u16(d,s+4),
    )
    mode = 'I;16L'
    im = Image.frombytes(mode,size,d[s+32:],decoder_name='raw')
    im = im.convert(mode='F')
    return(im)

def max_contrast(im, debug=False):
    pmin, pmax = im.getextrema()
    if debug:
        print(f'{pmin}, {pmax}')
    m = 255 / (pmax - pmin)
    b = m * pmin
    im = im.point(lambda x: m*x-b)
    im = im.convert(mode='L')
    if debug:
        stat = ImageStat.Stat(im)
        print(f'min/max: {stat.extrema}')
        print(f'mean: {stat.mean}  stddev: {stat.stddev}')
    return im

if __name__ == '__main__':
    fname = "Snap-000014.img"
    #im = read(fname)
    im = Image.open(fname)
    im = max_contrast(im, True)
    im.show()

Activity

  1. kamocat commented on Nov 8, 2023

    @kamocat
    Author

    Snap-000014.zip
    Here's the zipped test image. For some reason, FLIR decided to use the .img extension, rather than .fff which would have made sense.

  2. radarhere commented on Nov 11, 2023

    @radarhere
    Member

    Hi. You've said that you're using Pillow 9.4.0. However, the handbook instructions are for the most recent version of Pillow - you're setting _mode, which wasn't introduced until 10.1.0 with #7307. I would recommend upgrading your version of Pillow, unless you have a reason not to.

  3. kamocat commented on Nov 12, 2023

    @kamocat
    Author

    How embarrassing. The issue is fixed with version 10.

    Next to see about extracting enough metadata to actually determine the temperature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions