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

8bit grayscale now loaded as RGB #1032

Open
petsuter opened this issue Aug 23, 2023 · 5 comments
Open

8bit grayscale now loaded as RGB #1032

petsuter opened this issue Aug 23, 2023 · 5 comments

Comments

@petsuter
Copy link

Hi,

Example file: https://github.com/sondrele/rust-bmp/raw/master/test/bmpsuite-2.5/g/pal8gs.bmp

Such grayscale images are now loaded as RGB:

>>> import imageio
>>> imageio.imread("pal8gs.bmp").shape
(64, 127, 3)
>>> imageio.__version__
'2.31.1'

They used to be loaded as grayscale:

>>> import imageio
>>> imageio.imread("pal8gs.bmp").shape
(64, 127)
>>> imageio.__version__
'2.6.1'

Is this a bug / intended? Can it be disabled / controlled somehow?

Thanks.

@FirefoxMetzger
Copy link
Contributor

Interesting find, thanks for the report @petsuter . I'll try to have a look tomorrow and see if I can find the reason for the behavior change.

In the meantime, you can "force" a grayscale result by using:

>>> import imageio.v3 as iio
>>> iio.imread("https://github.com/sondrele/rust-bmp/raw/master/test/bmpsuite-2.5/g/pal8gs.bmp", mode="L").shape
(64, 127)

@petsuter
Copy link
Author

Thanks, that's very helpful to know. 👍 Is that documented somewhere? Is it specific to Pillow?


As far as I can tell:

    elif im.mode == "P" and is_gray:
        # Paletted images that are already gray by their palette
        # are converted so that the resulting numpy array is 2D.
        frame = im.convert("L")
        elif image.mode == "P":
            # adjust for pillow9 changes
            # see: https://github.com/python-pillow/Pillow/issues/5929
            image = image.convert(image.palette.mode)

(Where image.palette.mode is 'RGB'.)

So maybe this changed because the legacy Pillow plugin is not used anymore, maybe since #824 (mentioned as the change of v2.28.0)?

@petsuter
Copy link
Author

petsuter commented Aug 28, 2023

I'd also like to read 16 bit grayscale images in the same code. Example: https://github.com/lunapaint/pngsuite/blob/main/png/basi0g16.png

>>> imageio.v3.imread("basi0g16.png").dtype
dtype('int32')
>>> imageio.v3.imread("basi0g16.png", mode="L").dtype
dtype('uint8')
>>> imageio.v3.imread("basi0g16.png", mode="I").dtype
dtype('int32')
>>> imageio.v3.imread("basi0g16.png", mode="I;16").dtype
dtype('uint16')
>>> imageio.v3.improps("basi0g16.png")
ImageProperties(shape=(32, 32), dtype=dtype('int32'), n_images=None, is_batch=False, spacing=None)
>>> imageio.v3.immeta("basi0g16.png")
{'interlace': 1, 'gamma': 1.0, 'mode': 'I', 'shape': (32, 32)}

So using mode="L" would be problematic; mode="I;16" would be required there.
Would I have to try "I;16" first and fallback to "L", or is there a better way?
Can one assume improps().dtype=='int32' means "I;16" should be used?
Thanks!

@petsuter
Copy link
Author

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    imageio.v3.imread(r"test.tif", mode="L")
  File "C:\Python311\site-packages\imageio\v3.py", line 54, in imread
    return np.asarray(img_file.read(**call_kwargs))
  File "C:\Python311\site-packages\imageio\plugins\tifffile_v3.py", line 189, in read
    ndimage = self._fh.asarray(series=index, **kwargs)
TypeError: TiffFile.asarray() got an unexpected keyword argument 'mode'

Is mode backend specific? Is there a generic way?
Thanks.

@Pandede
Copy link
Contributor

Pandede commented Sep 25, 2023

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    imageio.v3.imread(r"test.tif", mode="L")
  File "C:\Python311\site-packages\imageio\v3.py", line 54, in imread
    return np.asarray(img_file.read(**call_kwargs))
  File "C:\Python311\site-packages\imageio\plugins\tifffile_v3.py", line 189, in read
    ndimage = self._fh.asarray(series=index, **kwargs)
TypeError: TiffFile.asarray() got an unexpected keyword argument 'mode'

Is mode backend specific? Is there a generic way? Thanks.

You may change the plugin to read the image:

imageio.v3.imread('test.tif', mode='L', plugin='pillow')

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

No branches or pull requests

3 participants