Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image plugin fails despite correct tile settings #7536

Closed
kamocat opened this issue Nov 8, 2023 · 3 comments
Closed

Image plugin fails despite correct tile settings #7536

kamocat opened this issue Nov 8, 2023 · 3 comments

Comments

@kamocat
Copy link

kamocat commented Nov 8, 2023

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()
@kamocat
Copy link
Author

kamocat commented Nov 8, 2023

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.

@radarhere
Copy link
Member

radarhere commented Nov 11, 2023

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.

@kamocat
Copy link
Author

kamocat commented Nov 12, 2023

How embarrassing. The issue is fixed with version 10.

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

@kamocat kamocat closed this as completed Nov 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants