[英]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'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.