PYTHON - Load random images from a folder without knowing the file name.

edited October 2017 in Python Mode

I'm wanting to make a system where one sketch creates an image and sends it into a folder where a second sketch randomly selects and loads one of the images in the folder. I know how to load images but how would I do it without knowing the file name.

(I'm also coding in python not js)

Tagged:

Answers

  • edited October 2017 Answer ✓
    • Processing got an undocumented function called listPaths().
    • For its 1st parameter, we pass the path of the target folder as a String.
    • For its 2nd parameter we can pass 1 String w/ all the desired extensions.
    • Then it returns a String[] array of all paths from that folder path fully filtered out.
    • However, given its undocumented, in order to access it under Python Mode, we need to prefix it w/ this.. :-<
    • Here's a wrapper function for listPaths() I did, w/ all Processing's valid image extensions defined there already: \m/

    def getPathsFromFolder(folder, EXTS='extensions=,png,jpg,jpeg,gif,tif,tiff,tga,bmp'):
        return this.listPaths(folder, EXTS);
    

    Usage example: :-B

    def setup():
        global imagePaths
        imagePaths = getPathsFromFolder(this.dataPath(''))
    

    Replace the argument this.dataPath('') w/ the String folder path where your images are. O:-)

  • Ahh thats perfect. Thank you!

Sign In or Register to comment.