每像素 2 位 tga 颜色到 qRgb

发布于 2024-12-03 07:44:47 字数 1054 浏览 2 评论 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]值以创建正确格式和色彩空间的值?

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

I need to read tga's with pyqt and so far this seems to be working fine except where a tga has 2 bytes per pixel as opposed to 3 or 4. My code is taken from here http://pastebin.com/b5Vz61dZ.

Specifically this section:

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

and this part:

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

how would I input the pixel[0] and pixel[1] values to create get the values in the correct format and colorspace?

Any thoughts, ideas or help please!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕花眠 2024-12-10 07:44:47
pixel = [ pixel[1]*2 , pixel[1]*2 , pixel[1]*2 ]
color = qRgb(*pixel)

对我有用。正确的亮度等等。尽管我不确定将像素[1]值加倍是否适用于所有实例。

感谢您对 istepura 的所有帮助:)

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

works for me. Correct luminance and all. Though I'm not sure doubling the pixel[1] value would work for all instances.

Thank you for all the help istepura :)

忆梦 2024-12-10 07:44:47

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

“像素以小端顺序存储,采用 BGR555 格式。”

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

对于你的情况,我想代码应该是这样的:

pixel = [(pixel[1]&0xF8)>>3, ((pixel[1]&0x7)<<2)|((pixel[0]&0xC0)>>6), (pixel[0]&0x3E)>>1)
color = qRgb(*pixel)

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

"Pixels are stored in little-endian order, in BGR555 format."

So, you have to take "leftmost" 5 bits of pixel[1] as Blue, rest 3 bits + 2 "leftmost" bits of pixel[0] would be Green, and next 5 bits of pixel[0] would be Red.

In your case, I suppose, the code should be something like:

pixel = [(pixel[1]&0xF8)>>3, ((pixel[1]&0x7)<<2)|((pixel[0]&0xC0)>>6), (pixel[0]&0x3E)>>1)
color = qRgb(*pixel)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文