Image Art Gallery

I’m working on a virtual art gallery in which the user can walk around and look at different images in picture frames. I want the user to be able to put all the images they care to in a folder and they will be added to the gallery without having to recode anything.

The first step in this will be making a list of all the files in a directory, then weeding out all the pictures (probably with a list of file extensions) then applying each element of the list to the textures in each picture frame.

I just found out about python’s OS module and am still trying to get the hang of its basic commands. Can anyone show me a quick way to:

  1. Build a list of the items in a directory (with listdir() I assume)
  2. Filter the list so that only elements with, lets say, JPEG,JPG, and PNG files are in the list

If this is more suited for a strictly-python forum I can ask elsewhere, I just assume that some people here might have experience making a similar project.

shnippset from my egg->bam optimizer
i think it’s self explaining so i’ll skipp #comments^^

import os

files = os.listdir("./")
exportlist = []
for i in files:
	if i.endswith(".egg"):

would list put all files with a .egg extension in the current diretory into a list

in my case it’s a egg file… but you can load a texture with simmilar code like

for i in exportlist:
	modelRoot = loader.loadModel("./"+i)

which would load all my models i put into the list above

in your case it would be:

files = os.listdir("./")
list = []
for i in files:
	if i.endswith(".png") or i.endswith(".jpg") or i.endswith(".jpeg") :

… or simmilar… havent tested it but could work. perhaps you need to add more “or”'s for capital endings (so the user dont have to change endings manually)
you could also use a set instead of a list… but i dont think the user will add enough images to make this neccessary.

That worked like a charm, thanks!

Python is a great language, and it offers several ways to do this. Here is another one:

import glob
list = glob.glob( '*.jpg' )

Alright, I’ve gotten a bit along in the gallery. So far the program collects all the picture files in a directory, places them in frames, and hangs them in an automatically generated gallery.

My problem now is that all the frames are the same size. I made the model for the frame to fit a specific picture I was experimenting with it, and now I’d like to have a frame model that is 1x1 in scale so that I can load pictures that are either landscape OR portrait and the frame and canvas (which the picture is textured to) are rescaled accordingly.

I found this method in the documentation:

LVecBase2f NodePath::get_tex_scale(TextureStage *stage) const;

Description: Returns the scale set for the UV's for the given stage on the current node.
This call is appropriate for ordinary 2-d texture coordinates.

But I’m not really sure…
A) is this what I need to use
B) exactly how I would use it (example, please?)

So any suggestions, again, will be extremely appreciated.


You don’t need to mess with the texture scale, unless for some reason you wanted to make your pictures not fit exactly within their frames.

Just scale the frame itself, e.g. with something like frame.setScale(2, 1, 0.5).


You can query your textures’ dimension using :

x = yourTexture.getExpectedMipmapXSize(0)
y = yourTexture.getExpectedMipmapYSize(0)

It might be a bit simpler to use yourTexture.getXSize() and yourTexture.getYSize(). These methods return the size of the texture itself, which is pretty much the same thing as asking the size of mipmap level 0. :slight_smile:

However, both this interface and the getExpectedMipmapXSize(0) interface will return the size of the texture as it currently is, which might include any implicit scaling from a non-power-of-two original source image. If you want to get the size of the original image before it was scaled, you have to read it separately, for instance with the PNMImageHeader class:

p = PNMImageHeader()
x = p.getXSize()
y = p.getYSize()


Both the first and second method suggested gave me values after adjusted for powers of two, the third method gives me the right values for artx and arty that I need, but when I try to get a ratio between them I only get 0 and 1?

                    p = PNMImageHeader()
                    self.artx = p.getXSize()
                    self.arty = p.getYSize()
                    print self.artx
                    print self.arty
                    print 0.0 +self.arty/self.artx

After I figure this out and get proper x/y scaling I’ll post the program so far so if anyone wants to run it and give me any suggestions about optimizing the code or where to take the project next, they can!


This is due to integer division.
Both getXSize( ) and getYSize( ) return integers, and dividing int by int gives another int.
Try this:

float( self.arty ) / float( self.artx )