简体   繁体   English

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

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

Now I read a jpeg image using PIL, get the values of RGB. 现在,我使用PIL读取jpeg图像,获取RGB的值。

Although I can combine all the RGBs and then find the width and height that equal to the given value of rgb. 虽然我可以合并所有RGB,然后找到等于给定rgb值的宽度和高度。

Is there any more effective method or function achieve this goal? 有没有更有效的方法或功能可以实现这一目标?

美国天气雷达图

My ultimate goal is to get data of dBZ on this image, including latitude and longitude information. 我的最终目标是在此图像上获取dBZ的数据,包括纬度和经度信息。

So first step, I need to get coordinates in image equal to the given RGB. 所以第一步,我需要获取等于给定RGB的图像中的坐标。

Using NumPy's argwhere is a straightforward way to achieve what you want. 使用NumPy的argwhere是实现所需目标的直接方法。 For example, you could obtain the spatial coordinates of those pixels with RGB values [105, 171, 192] like this: 例如,您可以使用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)

The following snippet makes it apparent that the results above are correct: 下面的代码片段可以使上面的结果正确无误:

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

像素突出显示

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

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