0

I am currently trying to store the x, y coordinates of a black/white image. 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:

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!

Mauritius
  • 265
  • 1
  • 8
  • 23

1 Answers1

0

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]))
Mauritius
  • 265
  • 1
  • 8
  • 23