[英]Storing (x,y) coordinate of black pixels from bitmap in Python
I am currently trying to store the x
, y
coordinates of a black/white image. 我目前正在尝试存储黑白图像的
x
, y
坐标。 I would like to get a list like 我想要一个清单
black_pixel_coordinates = [(1,1), (5,20), (3,90), ...]
My current way of doing it is not working. 我目前的操作方式无效。 Images all have a 150x150 size This is my code:
图片的大小均为150x150。这是我的代码:
from PIL import Image
map_foto = Image.open(wk_dir+"/"+map_name)
map_bit=map_foto.tobitmap()
pixels = list(map_bit.getdata())
for y in range(150):
for x in range(150):
if pixels[x,y] == (0, 0, 0):
pixels = black_pixels
black_pixel_coordinates.append((x,y))
Somehow it does not work and throws the error: 不知何故,它不起作用并引发错误:
TypeError: list indices must be integers or slices, not tuple
I am new to programming and hope anyone here could help me with that issue. 我是编程新手,希望这里的任何人都可以帮助我解决该问题。 Thanks!
谢谢!
Found a solution now: 现在找到一个解决方案:
arr = np.asarray(map_bit)
black_pixels = np.array(np.where(arr == 0))
black_pixel_coordinates = list(zip(black_pixels[0],black_pixels[1]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.