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
[Bug]: I give an RGB image to imsave but I don't have the right color map! #29183
The source of the problem is that in figimage we only check for RGB if the input is already a numpy array. Is there a reason you need to work with list-of-list-of-list? Using numpy (which I is already a numpy array) the code becomes simpler (no nested loop), faster (numpy does array work at or near c-speed), and avoids the bug:
importmatplotlib.pyplotaspltimportnumpyasnpI=plt.imread("notre-dame.jpg")
J=np.zeros_like(I)
# RGB version of the image with only one plane non-zeroJ[..., 0] =I[..., 0]
K=np.zeros_like(I)
K[..., 0] =I[..., 1]
L=np.zeros_like(I)
L[..., 0] =I[..., 2]
# share the axis so that panning / zooming on one pans/zooms on allfig, axs=plt.subplots(4, figsize=(28, 10), sharex='all', sharey='all')
# loop over axes, data, and title stringsforax, data, titleinzip(
axs,
(I, J, K, L),
("Image complete", "Composante rouge", "Composante bleue", "Composante verte"),
):
ax.imshow(data)
ax.set_title(title)
fig.suptitle("Image de Notre-Dame", x=0.51, y=0.93)
fig.savefig("Notre-Dame_composante_ex1.png", dpi=750, bbox_inches="tight")
fig.savefig("Notre-Dame_composante_ex1.pdf", dpi=750, bbox_inches="tight")
plt.imsave("Notre-Dame_rouge.png", J) # The problem is here!plt.show()
As a short-term fix you can also do np.array(J) before passing to fig.imsave which will go through the correct code path.
This issue is suited to new contributors because it does not require understanding of the
Matplotlib internals. To get started, please see our contributing
guide.
We do not assign issues. Check the Development section in the sidebar for linked pull
requests (PRs). If there are none, feel free to start working on it. If there is an open PR, please
collaborate on the work by reviewing it rather than duplicating it in a competing PR.
This is a good first issue as it should require no API choices and we have a good reference for the expected behavior already in the code base.
The issue is that when passing a list-of-list-of-list to plt.imsave and saving to a format that pillow and write the check that sees if it is an RGB image (which should not be color mapped) or a 1-channel luminescence image (which should) is avoided and we always color map.
The steps are:
look at Axes.imshow to see how we do the input normalization there
do the same thing is matplotlib.image.imsave
add a test (round-trip with imread and a small synthetic RGB image)
Thank you so much for your help...it is very useful, I will try to use arrays!
The point is that I am in preparation class in France and parts of the code are strictly imposed by the teacher!
Now it's just a code issue...I don't understand why I get that:
Instead of that:
with your code @tacaswell !
And by the way how can i save my lists in rgb files ...if it's possible?
I tried to convert them in numpy.arrays like that: plt.imsave("Notre-Dame_rouge.png", np.array(J))
but it returned that: ValueError: Image RGB array must be uint8 or floating point; found int64
Thank you again for your help!
Below is the full code for the script, including the fix for the imsave function to handle nested lists properly. I included headers to describe exactly what each code block function as and how it was changed.
@aragan22 This is not adding meaningfully to the issue. You are only rewriting the original restating the original minimal example with comments - but the example and issue is well understood. Nobody with the capability of fixing the issue needs these comments. Please think carefully to not post repetitive or unneeded stuff.
Apologies, I did not mean to comment that portion. That was simply for myself as I tried to understand the issue and work around it in my own way. Thank you for pointing this out, apologies again for the confusion.
Activity
tacaswell commentedon Nov 25, 2024
The source of the problem is that in
figimagewe only check for RGB if the input is already a numpy array. Is there a reason you need to work with list-of-list-of-list? Using numpy (whichIis already a numpy array) the code becomes simpler (no nested loop), faster (numpy does array work at or near c-speed), and avoids the bug:As a short-term fix you can also do
np.array(J)before passing tofig.imsavewhich will go through the correct code path.github-actions commentedon Nov 25, 2024
Good first issue - notes for new contributors
This issue is suited to new contributors because it does not require understanding of the
Matplotlib internals. To get started, please see our contributing
guide.
We do not assign issues. Check the Development section in the sidebar for linked pull
requests (PRs). If there are none, feel free to start working on it. If there is an open PR, please
collaborate on the work by reviewing it rather than duplicating it in a competing PR.
If something is unclear, please reach out on any of our communication
channels.
tacaswell commentedon Nov 25, 2024
This is a good first issue as it should require no API choices and we have a good reference for the expected behavior already in the code base.
The issue is that when passing a list-of-list-of-list to
plt.imsaveand saving to a format that pillow and write the check that sees if it is an RGB image (which should not be color mapped) or a 1-channel luminescence image (which should) is avoided and we always color map.The steps are:
Axes.imshowto see how we do the input normalization therematplotlib.image.imsaveimreadand a small synthetic RGB image)Frostbyte72 commentedon Nov 25, 2024
Hi I am a new contributor to opensource and believe I can fix this could I be assigned to this issue?
tacaswell commentedon Nov 25, 2024
We do not assign issues.
Please review https://matplotlib.org/stable/devel/index and open a PR when you are ready!
zef126 commentedon Nov 25, 2024
Thank you so much for your help...it is very useful, I will try to use arrays!
The point is that I am in preparation class in France and parts of the code are strictly imposed by the teacher!
zef126 commentedon Nov 25, 2024
It is the first repo where i fond kindness and attention, it is really appreciated!
zef126 commentedon Nov 25, 2024
Now it's just a code issue...I don't understand why I get that:


Instead of that:
with your code @tacaswell !
And by the way how can i save my lists in rgb files ...if it's possible?
I tried to convert them in numpy.arrays like that:
plt.imsave("Notre-Dame_rouge.png", np.array(J))but it returned that:
ValueError: Image RGB array must be uint8 or floating point; found int64Thank you again for your help!
QuLogic commentedon Nov 26, 2024
KandLshould use 1 and 2, respectively.tacaswell commentedon Nov 26, 2024
🤦🏻
as noteed, should be
and
np.asarray(J, dtype=np.int8)zef126 commentedon Nov 26, 2024
Thank you very much!
I just got this for saving the list of list of list:
10 remaining items
aragan22 commentedon Feb 18, 2025
Apologies, I did not mean to comment that portion. That was simply for myself as I tried to understand the issue and work around it in my own way. Thank you for pointing this out, apologies again for the confusion.
Dronten commentedon Mar 22, 2025
is this issue still up and can i work in this ?
rcomer commentedon Mar 22, 2025
@Dronten yes it looks like #29203 has stalled so you are welcome to go ahead. Please see our contributors’ guide:
https://matplotlib.org/devdocs/devel/index.html
Aftabby commentedon Apr 8, 2025
Has the issue been solved? Else I'd like to take over.
timhoffm commentedon Apr 9, 2025
@Aftabby it looks like #29203 has stalled so you are welcome to go ahead. Please see our contributors’ guide:
https://matplotlib.org/devdocs/devel/index.html
guillermodotn commentedon Apr 16, 2025
I have opened PR #29931 for this issue, based on the latest code review in #29203.
jk2500 commentedon May 6, 2025
Hey there, I tried running the code in the snippet and it shows the expected output. Does this mean the issue can be closed?
timhoffm commentedon May 7, 2025
Yes, closed by #29931.