Skip to content

NumPy array computed using vector multiplication not correctly converted #5227

Description

@thomas-maschler

What did you do?

I am trying to create a PNG image from a Numpy Array. Before converting the array, I transformed values using multiplication.

What did you expect to happen?

I would expect that the array values used as input correspond 1 to 1 with the output, no matter how they were computed.

What actually happened?

When I multiply my initial array using a vector, the final image has wrong pixel values (lots of zeros). However, if I replace the vector with a scalar the pixel values are correct.

What are your OS, Python and Pillow versions?

  • OS: MacOS
  • Python: 3.8
  • Pillow: 8.0.1
import numpy as np
from PIL import Image

vector = np.ones((5, 5, 3)).astype("uint8") * np.array([3])
scalar = np.ones((5, 5, 3)).astype("uint8") * 3

assert np.array_equal(vector, scalar)

vector_img = Image.fromarray(vector, mode="RGB")
scalar_img = Image.fromarray(scalar, mode="RGB")

assert np.array_equal(np.array(vector_img), np.array(scalar_img))

Activity

  1. radarhere commented on Jan 26, 2021

    @radarhere
    Member

    Hi. Removing mode="RGB" from your code,

    vector_img = Image.fromarray(vector)

    gives me

    TypeError: Cannot handle this data type: (1, 1, 3), <i8
    

    So my diagnosis is that this is just a form of data that we haven't added support for yet. I believe it corresponds to RGB mode with RGB;64S rawmode.

    Let us know if you think that it should still be working with mode="RGB" at the end.

  2. thomas-maschler commented on Jan 26, 2021

    @thomas-maschler
    Author

    Ah, I see.

    While values are identical, the different between the two arrays is the data type. vector array is cast to int64 while the scalar remains in uint8. I did not realize that.

    I would expect that pillow either throws a similar error message about the input data type as the one you posted above when using mode="RGB" or casts the values to uint8 correctly as long as they are in the 0-255 value range. Right now, it seems to set every other value to 0 although they are all the same.

  3. radarhere commented on May 1, 2021

    @radarhere
    Member

    I think the fact that an error isn't displayed can be considered part of #2856, and the lack of support for this format part of #1888

  4. changed the title [-]Numpy array computed using vector multiplication not correctly converted[/-] [+]NumPy array computed using vector multiplication not correctly converted[/+] on Mar 30, 2023
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