On 5-1-2010 9:57, Jef Mangelschots wrote:
> PIL 1.1.6 supposedly has a module name ImageGL to convert image files
> to OpenGL textures.
> This somehow does not work for me for lack of the _imaginggl module.
> This is supposedly part of imToolkit according to the comment but
> google never heard of it.
>
> How can I convert image files (e.g. JPG) to PyOpenGL textures ?
> can I do it with PIL (if so, how) or do I need something else ?
>
> Jef
Hello Jef,
I am using the following function to load PNG files into OpenGL using PIL:
def initPixelData(filename, tex_type = GL_TEXTURE_RECTANGLE_ARB):
img = Image.open(filename)
pixels = array(img.getdata(), 'B')
w, h = img.size
pixels = pixels[::-1].ravel() # -> pixels.reverse()
# this probably does not always work, but at least it works for RGB
and RGBA
exec("mode = GL_"+img.mode)
# Create texture / set texture parameters / put the data into the
texture / disable texture and return
texid = int(glGenTextures(1))
glActiveTexture(GL_TEXTURE0)
glEnable(tex_type)
glBindTexture(tex_type, texid)
glTexParameterf(tex_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameterf(tex_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexImage2D(tex_type, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE,
pixels)
glBindTexture(tex_type, 0)
return texid, w, h
You might have to alter it a bit, depending on the type of your images,
but it should give you a general idea how to do it.
Regards, Gijs
|