Question on RGB

Hi, I’m trying not to use the Python Image Library to show images in my GUI by Loading rgb file using
Is this feasible and will it work across platforms.
Here’s some code I whipped up quickly, but it produces a size msimatch error at the second to last line:

def DrawRGB(filename, px, py):
glRasterPos2f( px, py)
imgdata = rgbimg.longimagedata(filename)
imgsize = rgbimg.sizeofimage(filename)
w = imgsize[0]
h = imgsize[1]
buf = Buffer(GL_BYTE, (w, h , 4), imgdata)
glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, buf)


Blend On

in the script list effort you can find a display tga macro to use as an example, though there’s a mistake in the code (last line of the tga is black)

The reason you get a size mismatch is because you’re trying to assign a string, basically a continous array, and since it is a string, you cannot assign it directly to the buffer either, you have to convert it to bytes first. The data order is also not RGBA but ABGR, so you also have to swap bytes. It could be something like this:

def DrawRGB(filename, px, py):
  glRasterPos2f(px, py)
  imgdata = rgbimg.longimagedata(filename)
  w, h = rgbimg.sizeofimage(filename)
  buf = Buffer(GL_BYTE, len(imgdata))
  for i in range(0, len(imgdata), 4):
    buf[i] = ord(imgdata[i+3])
    buf[i+1] = ord(imgdata[i+2])
    buf[i+2] = ord(imgdata[i+1])
    buf[i+3] = ord(imgdata[i])
  glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, buf)

This conversion can take a while if the image is large, you can make this a bit simpler and faster by using the struct module to unpack the string and using an opengl extension ‘GL_EXT_ABGR’ to do the byte swapping for you on display.

As far as platform issues is concerned, the rgbimg module documentation states that it is only available on 32bit platforms, so if this is important, you could try loading raw tga files instead using jms’s tga load function. The black line error in the code is easy to fix, just change the zero in ‘for y in range(height-1,0,-1)’ to -1

The same principle applies here too, you can use opengl to do the tga argb to rgba conversion for you and also use the array module to load the data faster.
There is code in the last LFexport script (not the old one from my stormpages site) which did this as well, displaying a tga image in a window of any size preserving aspect ratio.

Thanks for the help man. I’m printing this to have a good look at it.
I solved the problem of not using PIL a while back by writing a script that uses PIL that the developer (me) uses to create a rgb image that is stored in an array in a .py file. This is what I used to create the beast file. I’m thinking about incorporating RLE compression into it and giving the created .py file the ability to initialize and draw itself. What do you think about the idea. This way the only person who needs PIL is the developer and the user need not worry about the added complexity of installing it.

blend on man and thanks again