0

我需要用 pyqt 读取 tga,到目前为止,这似乎工作正常,除非 tga 每个像素有 2 个字节,而不是 3 或 4 个。我的代码取自这里http://pastebin.com/b5Vz61dZ

特别是本节:

def getPixel( file, bytesPerPixel):
    'Given the file object f, and number of bytes per pixel, read in the next pixel and return a     qRgba uint'
    pixel = []
    for i in range(bytesPerPixel):
        pixel.append(ord(file.read(1)))

    if bytesPerPixel==4:
        pixel = [pixel[2], pixel[1], pixel[0], pixel[3]]
        color = qRgba(*pixel)
    elif bytesPerPixel == 3:
        pixel = [pixel[2], pixel[1], pixel[0]]
        color = qRgb(*pixel)
    elif bytesPerPixel == 2:
        # if greyscale
        color = QColor.fromHsv( 0, pixel[0] , pixel[1])
        color = color.value()

    return color

这部分:

elif bytesPerPixel == 2:
    # if greyscale
    color = QColor.fromHsv( 0, pixel[0] , pixel[1])
    color = color.value()

我将如何输入像素 [0] 和像素 [1] 值来创建以正确格式和色彩空间获取值?

任何想法,想法或帮助请!!!

4

2 回答 2

1
pixel = [ pixel[1]*2 , pixel[1]*2 , pixel[1]*2 ]
color = qRgb(*pixel)

为我工作。正确的亮度和所有。虽然我不确定将像素 [1] 值加倍是否适用于所有实例。

感谢您提供的所有帮助 istepura :)

于 2011-09-07T12:06:39.240 回答
0

http://lists.xcf.berkeley.edu/lists/gimp-developer/2000-August/013021.html

“像素以 BGR555 格式以 little-endian 顺序存储。”

因此,您必须将像素 [1] 的“最左侧” 5 位作为蓝色,其余 3 位 + 像素 [0] 的 2 个“最左侧”位将是绿色,而像素 [0] 的下 5 位将是红色。

在你的情况下,我想,代码应该是这样的:

pixel = [(pixel[1]&0xF8)>>3, ((pixel[1]&0x7)<<2)|((pixel[0]&0xC0)>>6), (pixel[0]&0x3E)>>1)
color = qRgb(*pixel)
于 2011-09-06T13:30:51.950 回答