Skip to content

Problems with other data-types than uint8 #4887

Description

@emilettele

What did you do?

I was trying to create an Image from a numpy array using Image.fromarray(array, mode) and validate the created image by using the show() function. It did not create the right image.

What did you expect to happen?

Show a white image

What actually happened?

Showed an image with seperated colors.

What are your OS, Python and Pillow versions?

  • OS: Windows
  • Python: 3.6.8
  • Pillow: 7.2.0

What is the problem?

Pillow shows the right image if the dtype of the values in the array is uint8, but seems to have problems accepting other data types.

import numpy as np
from PIL import Image

array = np.full((32,32,3),255)
array[0].dtype
image = Image.fromarray(array, "RGB")
image.show()

wrong

It does however work if I first convert the array to uint8:

import numpy as np
from PIL import Image

array = np.full((32,32,3),255)
array = np.uint8(array)
array[0].dtype
image = Image.fromarray(array, "RGB")
image.show()

right

Activity

  1. nulano commented on Aug 24, 2020

    @nulano
    Contributor

    Related to #1888.

  2. emilettele commented on Aug 25, 2020

    @emilettele
    Author

    After going through some of the related issues I now see that @wiredfool commented on 12 Jan 2018 in #2955:
    "That's a 16 bit per channel RGB image, and Pillow doesn't support multi channel images with more than 8 bpc. It's a longstanding issue."

    In my opinion, it would be nice if there would at least be an error text that can explain this limitation of 8bpc. Also, this limitation could be made a lot more clear on the information pages. Since I did not know about this, it took me quite some time to fix the problem.

  3. changed the title [-]Pillow still seems to have problems with other data-types than uint8.[/-] [+]Problems with other data-types than uint8[/+] on Aug 25, 2020
  4. radarhere commented on May 1, 2021

    @radarhere
    Member

    I'll point out that an error is displayed - if you don't pass mode to fromarray.

    import numpy as np
    from PIL import Image
    
    array = np.full((32,32,3),255)
    Image.fromarray(array)
    Traceback (most recent call last):
      File "PIL/Image.py", line 2777, in fromarray
    KeyError: ((1, 1, 3), '<i8')
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "demo.py", line 5, in <module>
        Image.fromarray(array)
      File "PIL/Image.py", line 2779, in fromarray
    TypeError: Cannot handle this data type: (1, 1, 3), <i8
    
  5. radarhere commented on May 1, 2021

    @radarhere
    Member

    I think the fact that an error isn't displayed can be considered part of #2856

  6. radarhere commented on May 10, 2021

    @radarhere
    Member

    Also, this limitation could be made a lot more clear on the information pages. Since I did not know about this, it took me quite some time to fix the problem.

    Do you have any ideas where a good place for this documentation would be? https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes doesn't seem like the right place, since it is about the modes that Pillow uses to store image data, not about the modes used to read image data from files.

  7. radarhere commented on Oct 4, 2022

    @radarhere
    Member

    I've created PR #6638 to resolve this by documenting the current limitation.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions