If you want to have iteration along the x-axis, you can write like:
from PIL import Image
img = Image.open('img.png')
pixels = img.load()
width, height = img.size
for y in range(height): # this row
for x in range(width): # and this row was exchanged
r, g, b = pixels[x, y]
# in case your image has an alpha channel
# r, g, b, a = pixels[x, y]
print(x, y, f"#{r:02x}{g:02x}{b:02x}")
Thinking of the for
statement, x and y are going to change like (x,y) = (0,0), (0,1),... in your initial code.
You want to first iterate over x and then iterate over y; you can write iteration over y, wrapping iteration over x.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…