为什么QImage的像素功能超出范围?

时间:2020-06-09 04:57:17

标签: python qt qimage

我在另一个字行= 394和col = 700中具有高度= 394和宽度= 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))

1 个答案:

答案 0 :(得分:2)

QImage::pixel函数将(int x, int y)用作参数(column, row)