Ask Your Question

Revision history [back]

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

sage: E = PIL.Image.open('$HOME/Desktop/einstein.png')
sage: E.info
{'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 = PIL.Image.open('$HOME/Desktop/einstein.png')
sage: E.mode
'L'

"L" means grayscale: see https://pillow.readthedocs.io/en/stable/handbook/concepts.html#concept-modes.