You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Values change with implicit conversion from mode F to L #5465
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
Mode for fromarray is the "Mode to use". I think this is the mode to use when reading the numpy data - if so, then it makes sense that the values change when it is told to interpret the input data directly.
You might be interested in .convert('L') instead of , mode='L',
However, is it an expected behavior that random numbers appear in the matrix after doing the implicit conversion?
I assumed that the fromarray method either
calls the conversion script, if the input is np.float32, or,
raises an exception if the array.dtype does not match the specified mode.
I think that the mode to use parameter is a footgun. It’s not an implicit conversion, it’s a cast. In this case, you’re interpreting an array of 32 bit floats as 8 bit ints.
I’m not sure if there is a good use case at all for the mode parameter here, as we need to be pretty sure what we’re getting from numpy to interpret the array correctly.
You might assume this is equal to np.ones(shape=(2, 2)), but tgt.dtype is "float64" and tgt_im_np.dtype is "float32". So the dtype changes, but I'm not convinced it's a problem that Pillow doesn't have a different mode for all of numpy's different modes.
So if we remove that roundtrip from the original code, then
Activity
radarhere commentedon May 4, 2021
Mode for fromarray is the "Mode to use". I think this is the mode to use when reading the numpy data - if so, then it makes sense that the values change when it is told to interpret the input data directly.
You might be interested in
.convert('L')instead of, mode='L',gives
meyerjo commentedon May 4, 2021
Okay thanks for the workaround.
However, is it an expected behavior that random numbers appear in the matrix after doing the implicit conversion?
I assumed that the
fromarraymethod eitherwiredfool commentedon May 4, 2021
I think that the mode to use parameter is a footgun. It’s not an implicit conversion, it’s a cast. In this case, you’re interpreting an array of 32 bit floats as 8 bit ints.
I’m not sure if there is a good use case at all for the mode parameter here, as we need to be pretty sure what we’re getting from numpy to interpret the array correctly.
dtoniolo commentedon Sep 20, 2021
I had a similar issue in #5723. So, if
modeis given toImage.fromarray(), then what happens is:Fif thedtypeisnp.float32and the values all reside in the[0, 1].dtypeand value range of the data array if necessary.Is this correct?
radarhere commentedon Nov 22, 2021
To respond to the previous comment,
fromarraysets the mode according toPillow/src/PIL/Image.py
Lines 2877 to 2902 in d76fd93
So F mode is the "typestr" has "f" (if it is float, see https://numpy.org/doc/stable/reference/arrays.interface.html#object.__array_interface__ for more information)
Some notes on the original post.
This is not correct.
tgt.dtypeis "float64".gives
tgt_im_npofYou might assume this is equal to
np.ones(shape=(2, 2)), buttgt.dtypeis "float64" andtgt_im_np.dtypeis "float32". So thedtypechanges, but I'm not convinced it's a problem that Pillow doesn't have a different mode for all of numpy's different modes.gives
This seems more reasonable than the "random numbers" that were initially described.
gives
radarhere commentedon Nov 23, 2021
Closing unless there are further questions. #2856 is also about this situation.
radarhere commentedon Nov 23, 2021
I've created PR #5849 to clarify this in the documentation.