简体   繁体   English

从Python中的位图存储黑色像素的(x,y)坐标

[英]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. 我目前正在尝试存储黑白图像的xy坐标。 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.

相关问题 如何获取图像中像素的x,y(坐标)数组 - how to get x,y(coordinate) array of pixels in image 识别python x,y数组中的x,y坐标对 - identify x,y coordinate pair in a python x,y array Python:使用到点 A 的距离(x0,y0)和角度找到给定点 B 的 ax,y 坐标 - Python: find a x,y coordinate for a given point B using the distance from the point A (x0, y0) and the angle X,Y 坐标到 gcode - X,Y coordinate to gcode Python:从要提取的文本为黑色的图像中去除黑色像素 - Python: removing black pixels from an image where the text to be extracted is black 如何从 ply 文件中获取 python 中给定点 ( x, y ) 的 z 轴坐标? - How do I get z-axis coordinate from ply file for a given point ( x, y ) in python? 如何从 python 中的图像中获取特定像素(蓝色)的 x、y 坐标? - How to fetch x,y coordinates of specific pixels(blue colored) from an image in python? (字典)在Python中存储带有x,y的键 - (Dictionary) storing keys with x,y in Python 如何将图像数据分成x,y坐标? (蟒蛇) - How to separate image data into x, y coordinate? (python) 如何在Python中计算x / y坐标的长度? - How do I calculate the length of an x/y coordinate in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM