Skip to content

Image plugin fails despite correct tile settings #7536

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

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