How to convert Cartopy tiles in grayscale?
I’m trying to display the graph block downloaded via Cartopy’s img_tiles
module in grayscale. That’s because I want to overlay color-coded satellite data, and I prefer the background to be as undistracting as possible.
various graphs documented in source code Block providers are allowed to be used during initialization desired_tile_form
。 The documentation isn’t entirely clear on the possible values that can be assigned to that parameter (only mentioning “RGB”), but digging a bit in the code I found that it was passed to img.convert
, which is a PIL.image method.
Now PIL documentation says:
The current version supports all possible conversions between “L”,
“RGB” and “CMYK.” The matrix argument only supports “L” and “RGB”.When translating a color image to black and white (mode “L”), the
library uses the ITU-R 601-2 luma transform:
[…]
So it looks like using the “L” mode turns the image black and white, but in my case this doesn’t seem to work.
Example:
Standard “RGB” mode:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
figure, ax = plt.subplots(figsize=(15, 10))
request = cimgt. StamenTerrain()
ax = plt.axes(projection=request.crs)
ax.set_extent([94, 102, 0, 6])
ax.add_image(request, 8)
plt.show()
(Not working) Black and white mode:
figure, ax = plt.subplots(figsize=(15, 10))
request = cimgt. StamenTerrain(desired_tile_form="L")
ax = plt.axes(projection=request.crs)
ax.set_extent([94, 102, 0, 6])
ax.add_image(request, 8)
plt.show()
I got similar results with GoogleTiles().
What is the correct way to turn a tile black and white?
Solution
I’m not sure if this is by design, but it looks like the default colormap is being used here. You can pass different colormaps to the add_image()
method, for example:
ax.add_image(request, 8, cmap='gray')