Skip to content

Values change with implicit conversion from mode F to L #5465

@meyerjo

Description

@meyerjo

Might be somewhat related to for instance #3011

What did you do?

import numpy as np
from PIL import Image
tgt = np.ones(shape=(2, 2)) # float32
tgt_im = Image.fromarray(tgt)
tgt_im_np = np.asarray(tgt_im)
final_img = Image.fromarray(tgt_im_np, mode='L')

print(np.asarray(final_img))

What did you expect to happen?

I assumed that the resulting matrix values still have the value of 1 but are implictly converted to 8-bit range.

What actually happened?

>>> print(np.asarray(final_img))
[[  0   0]
 [128  63]]

None of the resulting values keep the original value of 1 and (for me even more surprising) they change to different values at different places in the matrix.

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 20.10
  • Python: Python 3.8.8
  • Pillow:
pip list
Package    Version
---------- -------------------
certifi    2020.12.5
numpy      1.20.2
Pillow     8.2.0
pip        21.0.1
setuptools 52.0.0.post20210125
wheel      0.36.2

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