Ask Your Question

A grayscale png image became color after importing to sage

asked 2022-08-09 21:11:41 +0200

Tati gravatar image

I used the following code to import a png grayscale image to sage. For some reason, when I display it, it is no longer grayscale (it is yellow/blue scale instead). I would appreciate any help figuring out why. Here is the code:



edit retag flag offensive close merge delete


Where are you importing imread and imshow?

John Palmieri gravatar imageJohn Palmieri ( 2022-08-10 20:00:12 +0200 )edit

Also, can you provide a sample grayscale file for us to test? Or instructions on how to produce one? I would like to try to reproduce the issue on my computer.

John Palmieri gravatar imageJohn Palmieri ( 2022-08-10 20:02:37 +0200 )edit

I was not allowed to attach anything, but it just dawned on me that I could attach a link. here is a link to the file in my drive: I am running sage from cocalc, so I uploaded the file into my project directory and used imread to work with it. I started out with a jpeg file, but in cocalc I got an error that it can't read jpeg. So I used a free on-line converter to convert it to png.

Here is the screenshot running the code:

Tati gravatar imageTati ( 2022-08-10 21:21:18 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2022-08-10 22:51:21 +0200

I think your solution is the right fix. First, the documentation for imread suggests using instead. Doing that yields the same result when you output it, but the image does seem to be labeled as grayscale:

sage: E ='$HOME/Desktop/einstein.png')
{'icc_profile': b'\x00\x00\x01\x98ADBE\x02\x10\x00\x00mntrGRAYXYZ \x07\xcf\x00\x06\x00\x03\x00\x00\x00\x00\x00\x00acspAPPL\x00\x00\x00\x00none\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf6\xd6\x00\x01\x00\x00\x00\x00\xd3-ADBE\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05cprt\x00\x00\x00\xc0\x00\x00\x002desc\x00\x00\x00\xf4\x00\x00\x00iwtpt\x00\x00\x01`\x00\x00\x00\x14bkpt\x00\x00\x01t\x00\x00\x00\x14kTRC\x00\x00\x01\x88\x00\x00\x00\x0etext\x00\x00\x00\x00Copyright 1999 Adobe Systems Incorporated\x00\x00\x00desc\x00\x00\x00\x00\x00\x00\x00\x0fGray Gamma 2.2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00XYZ \x00\x00\x00\x00\x00\x00\xf3T\x00\x01\x00\x00\x00\x01\x16\xcfXYZ \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00curv\x00\x00\x00\x00\x00\x00\x00\x01\x023\x00\x00',
 'dpi': (599.9988, 599.9988)}

The key is that the documentation for imshow says

For displaying a grayscale image set up the colormapping using the parameters "cmap='gray', vmin=0, vmax=255".

(I had better luck with just cmap='gray', without specifying vmin and vmax.)

So it appears to be a feature of imshow (and related functions) that you have to tell it to use grayscale when outputting the image.

More evidence that the file is read in as grayscale:

sage: E ='$HOME/Desktop/einstein.png')
sage: E.mode

"L" means grayscale: see

edit flag offensive delete link more

answered 2022-08-10 01:34:09 +0200

Tati gravatar image

I figure out how to fix it, but I still don't understand why it happens in the first place:

Here is a fix: imshow(img/255.0, cmap='gray') which shows it in grayscale.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2022-08-09 21:10:11 +0200

Seen: 172 times

Last updated: Aug 10 '22