简体   繁体   English

如何在灰度中转换 Cartopy 瓷砖?

[英]How to convert Cartopy tiles in grayscale?

I'm trying to display tiles downloaded via Cartopy's img_tiles module in gray scale.我正在尝试以灰度显示通过 Cartopy 的img_tiles模块下载的img_tiles That's because I would like to overlay color-coded satellite data and I would prefer a background as little distracting as possible.那是因为我想覆盖颜色编码的卫星数据,并且我希望背景尽可能少分散注意力。

The various tiles providers as documented in the source code allow the use of a desired_tile_form during initialization. 源代码中记录的各种desired_tile_form贴提供程序允许在初始化期间使用desired_tile_form The documentation is not entirely clear about the possible values one could assign to that parameter (only "RGB" is mentioned), but digging a bit in the code I see that it is passed to img.convert , which is a PIL.image method.文档并不完全清楚可以分配给该参数的可能值(只提到了“RGB”),但是在代码中挖掘了一下我看到它被传递给img.convert ,这是一个 PIL.image 方法.

Now the PIL documentation says:现在PIL 文档说:

The current version supports all possible conversions between “L”, “RGB” and “CMYK.”当前版本支持“L”、“RGB”和“CMYK”之间的所有可能转换。 The matrix argument only supports “L” and “RGB”.矩阵参数仅支持“L”和“RGB”。

When translating a color image to black and white (mode “L”), the library uses the ITU-R 601-2 luma transform: [...]将彩色图像转换为黑白(模式“L”)时,库使用 ITU-R 601-2 亮度变换:[...]

So it would appear that using the "L" mode would turn the image in black and white, however that doesn't really seem to work in my case.所以看起来使用“L”模式会将图像变成黑白,但是在我的情况下这似乎并不奏效。

Example:例子:

Standard "RGB" mode:标准“RGB”模式:

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()

RGB模式

(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 had similar results with GoogleTiles() .我用GoogleTiles()得到了类似的结果。

What is the correct way to turn tiles to black and white?将瓷砖变成黑白的正确方法是什么?

I'm not sure if it's by design, but it looks like the default colormap is being used here.我不确定它是否是设计使然,但看起来这里使用的是默认颜色图。 You can pass a different colormap to the add_image() method, eg:您可以将不同的颜色图传递给add_image()方法,例如:

ax.add_image(request, 8, cmap='gray')

Here's a list of colormaps available in Matplotlib. 这是 Matplotlib 中可用的颜色图列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM