E
Interpretation of the problemYou have an image that I will represent here as a list of strings. Each string represents a row and each character within the row represents a pixel. Dark pixels represent them with a 'x'; white pixels, with a space:# +----> eje X
# |
# |
# v eje Y
imagen = [
" x xxxxx xxx xx xxxx x",
" xxxx xxxx xx xxxxx xxxx ",
"x xx xxxx xx xxxxx xxxxx x",
" xxx xxxxxxx xxxxxx xxx xxxxx ",
" xx xxxx xx xxxxxx xxx xxxxxx x ",
"xxxx xxxxxxx xxx xxxxxx xxxx x",
" x xxxxx xxx xx xxxx ",
" xxxx xxxx xx xxxxx xxxx x",
"x xx xxxx xx xxxxx xxxxx ",
" xxx xxxxxxx xxxxxx xxx xxxxx x",
" xx xxxx xx xxxxxx xxx xxxxxx x ",
"xxxx xxxxxxx xxx xxxxxx xxxx x",
]
Then, the pixel in the coordinate (x, and) is represented by imagen[y][x]. I have to turn coordinates X and Y for a representational problem simply.Coordinate X varies from 0 to 37 (left to right) and coordinate Y from 0 to 11 (upside down).Following your code, I generate a list of all possible X coordinates in pnx. Same for coordinates Y, with pny. No mystery there.pnx=list(range(0, len(imagen[0])))
pny=list(range(0, len(imagen)))
I then make a list of 10 random coordinates:lx = random.choices(pnx, k=10)
ly = random.choices(pny, k=10)
In lista_blanca I've got a list of 10 tups. Each tube contains a coordinate (x, y). Generate these tups using the function zipwhich unites both lists:lista_blanca = zip(lx, ly)
The dark list empty part:lista_oscura = []
Now I collect the white list to obtain the value of each pixel in the corresponding coordinate:print("Examinar muestra aleatoria")
for x, y in lista_blanca:
pixel = imagen[y][x]
print(f"Pixel({x}, {y}) = {pixel}")
if pixel == 'x':
lista_oscura.append((x,y))
Remember that the pixel(x,y) is in imagen[y][x].In pixel I have the value of the pixel, which can be 'x' or '' (white). If it's a dark pixel, I add the coordinates to the dark list.That would be all.TestingHere is the full code, plus a check of the results:import random
+----> eje X
|
|
v eje Y
imagen = [
" x xxxxx xxx xx xxxx x",
" xxxx xxxx xx xxxxx xxxx ",
"x xx xxxx xx xxxxx xxxxx x",
" xxx xxxxxxx xxxxxx xxx xxxxx ",
" xx xxxx xx xxxxxx xxx xxxxxx x ",
"xxxx xxxxxxx xxx xxxxxx xxxx x",
" x xxxxx xxx xx xxxx ",
" xxxx xxxx xx xxxxx xxxx x",
"x xx xxxx xx xxxxx xxxxx ",
" xxx xxxxxxx xxxxxx xxx xxxxx x",
" xx xxxx xx xxxxxx xxx xxxxxx x ",
"xxxx xxxxxxx xxx xxxxxx xxxx x",
]
pnx=list(range(0, len(imagen[0])))
pny=list(range(0, len(imagen)))
lx = random.choices(pnx, k=10)
ly = random.choices(pny, k=10)
lista_blanca = zip(lx, ly)
lista_oscura = []
print("Examinar muestra aleatoria")
for x, y in lista_blanca:
pixel = imagen[y][x]
print(f"Pixel({x}, {y}) = {pixel}")
if pixel == 'x':
lista_oscura.append((x,y))
print("Listar lista oscura")
for x, y in lista_oscura:
pixel = imagen[y][x]
print(f"Pixel({x}, {y})={pixel}")
produces:Examinar muestra aleatoria
Pixel(29, 8) =
Pixel(31, 11) = x
Pixel(25, 3) =
Pixel(0, 1) =
Pixel(36, 9) =
Pixel(6, 3) =
Pixel(12, 8) = x
Pixel(9, 6) = x
Pixel(34, 4) =
Pixel(25, 0) =
Listar lista oscura
Pixel(31, 11)=x
Pixel(12, 8)=x
Pixel(9, 6)=x
For reference, here is the image with its coordinates---0123456789123456789123456789*1234567
00 x xxxxx xxx xx xxxx x
01 xxxx xxxx xx xxxxx xxxx
02 x xx xxxx xx xxxxx xxxxx x
03 xxx xxxxxxx xxxxxx xxx xxxxx
04 xx xxxx xx xxxxxx xxx xxxxxx x
05 xxxx xxxxxxx xxx xxxxxx xxxx x
06 x xxxxx xxx xx xxxx
07 xxxx xxxx xx xxxxx xxxx x
08 x xx xxxx xx xxxxx xxxxx
09 xxx xxxxxxx xxxxxx xxx xxxxx x
10 xx xxxx xx xxxxxx xxx xxxxxx x
11 xxxx xxxxxxx xxx xxxxxx xxxx x