0

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!!!

Jay
  • 3,373
  • 6
  • 38
  • 55

2 Answers2

1
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 :)

Jay
  • 3,373
  • 6
  • 38
  • 55
0

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)
istepura
  • 399
  • 3
  • 8
  • Thanks but with this code at one point pixel[0] = 148 and pixel[1] = 82. meaning that red, graan and blue channels would have different pixel values, even though the image is greyscale. Also please excuse me for being such a noob but your syntax doesn't seem to work. I'm using python 2.6, are you using a later version or is that just annotation? – Jay Sep 07 '11 at 08:45
  • @mushu. Ah. Haven't noticed that it's a grayscale image. Is it 8 bit of gray level + 8 bit of alpha? If so, then you can convert gray pixel[1] to rgb as mentioned in http://stackoverflow.com/q/835753/851055 – istepura Sep 07 '11 at 09:37
  • Thanks. I tried `pixel = [ pixel[1] , pixel[1] , pixel[1] ]` `color = qRgb(*pixel)` as the first post mentions, the luminance is incorrect but otherwise the first example I tried worked. Do you have any ideas on how to correct the luminance? – Jay Sep 07 '11 at 11:54