<p>您可以非常简单地使用Numpy来实现这一点,Numpy是Python中大多数图像处理库的基础,例如<strong>OpenCV</strong>,或<strong>skimage</strong>,或<strong>Wand</strong>。在这里,我将使用OpenCV<strong>OpenCV</strong>,但您同样可以使用上述任何一种或PIL/枕头</p>
<p>使用右侧有一条蓝线的图像:</p>
<p><a href="https://i.stack.imgur.com/obcH9.png" rel="noreferrer"><img src="https://i.stack.imgur.com/obcH9.png" alt="enter image description here"/></a></p>
<pre><code>#!/usr/bin/env python3
import cv2
import numpy as np
# Load image
im = cv2.imread('image.png')
# Define the blue colour we want to find - remember OpenCV uses BGR ordering
blue = [255,0,0]
# Get X and Y coordinates of all blue pixels
X,Y = np.where(np.all(im==blue,axis=2))
print(X,Y)
</code></pre>
<p><strong>输出</strong></p>
<pre><code>[ 0 2 4 6 8 10 12 14 16 18] [80 81 82 83 84 85 86 87 88 89]
</code></pre>
<p>或者,如果希望将它们压缩到单个数组中:</p>
<pre><code>zipped = np.column_stack((X,Y))
array([[ 0, 80],
[ 2, 81],
[ 4, 82],
[ 6, 83],
[ 8, 84],
[10, 85],
[12, 86],
[14, 87],
[16, 88],
[18, 89]])
</code></pre>
<hr/>
<p>如果您喜欢使用PIL/枕头,它将如下所示:</p>
<pre><code>from PIL import Image
import numpy as np
# Load image, ensure not palettised, and make into Numpy array
pim = Image.open('image.png').convert('RGB')
im = np.array(pim)
# Define the blue colour we want to find - PIL uses RGB ordering
blue = [0,0,255]
# Get X and Y coordinates of all blue pixels
X,Y = np.where(np.all(im==blue,axis=2))
print(X,Y)
</code></pre>