我的图像的高度为394,宽度为700,其他字行为394,col为700。但是当我尝试通过grey_img.pixel(row, col)
访问给定像素的像素值时,出现了超出范围的错误。由于某些原因,grey_img.pixel(col, row)
有效。
有人可以帮我弄清楚,为什么我在进行grey_img.pixel(row, col)
时会出现超出范围的错误,而不是反过来?
我认为索引将以(row,col)开头,因为
((0,0),(0,1),(0,2)...(0,699)
((1,0),(1,1),(1,2)...(1,699)
...
((393,0),(393,1),(393,2)...(393,699)
from PyQt5.QtGui import QImage, qGray
# read jpg image and convert it to grey scale.
grey_img = QImage("test_img.jpg").convertToFormat(QImage.Format_Grayscale8)
# print the pixel value of each pixel form the grey scale image.
img_height = grey_img.height()
img_width = grey_img.width()
print("height = ", img_height)
print("width = ", img_width)
# height = 394
# width = 700
for row in range(0, img_height):
for col in range(0, img_width):
# works
gray_val = qGray(grey_img.pixel(col, row))
# does not work
# gives out of range error
# QImage::pixel: coordinate (179,482) out of range
# gray_val = qGray(grey_img.pixel(row, col))