Loading TGAs into a script

Has anyone done anything with loading TGA’s into the openGL canvas of a python Script? I have started to do some playing around with ‘glDrawPixel’ and some others and have met with limited success. Any tips you might have would be appreciated.

jms has a script that loads and display tgas into the text window. it works with raws (where you can read the pixeldata unchanged from a stream).

in general it goes like this (opengl only supports images with width == height): you create a buffer, load your image data into it and display it with glDrawPixels. i use PIL for loading the images, so my code looks like this:


img = PIL.Image.open("images/image.tga")
img.load()
bb = img.getbbox()
imgw = bb[2] + bb[0]
imgh = bb[3] + bb[1]

buf = BGL.Buffer(BGL.GL_BYTE, (imgw, imgh, 4))

for y in xrange(0, imgh):                   
    for x in xrange(0, imgw):                 
        d = img.getpixel((x, imgh - y - 1))
        buf[y][x][0] = d[0]                   
        buf[y][x][1] = d[1]                   
        buf[y][x][2] = d[2]

BGL.glDrawPixels(imgw, imgh, BGL.GL_RGBA, BGL.GL_UNSIGNED_BYTE, buf)


when you have the python image library installed it should work like above. don’t forget to set the position (glRasterPos2d) of the image, or it’s drawn in the left lower corner.

corban

(opengl only supports images with width == height):

I know I don’t post here much, and I haven’t done a whole lot with Python scripts, but I do consider myself an OpengL guru and had to clear up this falacy…

If you are using textures, OpenGL requires that the width and the height be a power of 2. They do NOT have to be equal. For instance 256x64 is a perfectly valid texture size because both 64 and 256 are powers of 2.

Now, when you are talking about glDrawPixels, you’re talking about something separate from textures (which are created with the glTexImage* functions). glDrawPixels has no size limitations on the dimensions.

I’m not sure about how the loading routines provided here work, but if you are doing your own TGA loading routine to get the raw RGB data, you should also remember that TGAs store their data in BGR format like BMP files, not RGB. You should also be checking the header to see if an alpha channel is present, if RLE encoding is being used, and a number of other things…

You have to check for the starting point too, if it’s the lower left corner or the upper left or whatever.

Martin

Without PIL tools :
http://jmsoler.free.fr/didacticiel/blender/tutor/def_tga_pic.htm
http://jmsoler.free.fr/didacticiel/blender/tutor/write_tga_pic.htm
loading the image in a quad :
http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_charger1image_en.htm
Shorter version at the end of the french page (no language problem, only a python script example ) :
http://jmsoler.free.fr/didacticiel/blender/tutor/cpl_charger1image.htm

As far as speed go’s Id recomment loading the image into blender and drawing it on a quad. its a lot less hassel and requires no external libs.
Its also faster on many GFX cards.

  • Cam