使用PIL(Python)在jpeg图像中找到RGB的给定值?

Using PIL (Python) to find the given value of RGB in jpeg image?

现在我使用 PIL 读取 jpeg 图像,获取 RGB 值。

虽然我可以组合所有的RGB,然后找到等于rgb给定值的宽度和高度。

有没有更有效的方法或功能实现这个目标?

我的最终目标是获取这张图片dBZ的数据,包括经纬度信息。

所以第一步,我需要在图像中获取等于给定 RGB 的坐标。

使用 NumPy 的 argwhere 是实现您想要的目标的直接方法。例如,您可以像这样获取具有 RGB 值 [105, 171, 192] 的那些像素的空间坐标:

In [118]: from skimage import io

In [119]: import numpy as np

In [120]: img = io.imread('https://i.stack.imgur.com/EuHas.png')

In [121]: ref = [105, 171, 192] 

In [122]: indices = np.argwhere(np.all(img == ref, axis=-1))

In [123]: indices
Out[123]: 
array([[ 71, 577],
       [ 79, 376],
       [ 79, 386],
       [ 95, 404]], dtype=int64)

以下代码片段表明上述结果是正确的:

import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2)

ax1.imshow(img)
ax1.set_title('Original map')
ax1.set_axis_off()

ax2.imshow(img)
ax2.set_title('Pixels with RGB = ' + str(ref))
for y, x in indices:
    ax2.add_artist(plt.Circle((x, y), 8, color='r'))