Forum Archive

List of all built-in graphics via Python?

Bortels

I know I can hit "+" to get a drop down with the built-in images and sounds - but is there some built-in way to enumerate that list without me manually transcribing them? Ideally a built-in, but even a gist with the lists would be cool. (I want to present them in a menu for the user to choose).

TutorialDoctor

There was a file browser script I downloaded somewhere from here that lets you brows all files related to Pythonista, including the images. I was playing with it last night.

Edit: Found it

https://github.com/dgelessus/pythonista-scripts/blob/master/filenav.py

dgelessus

Obscure oneliner incoming:

import os

builtin_images = [name for name in os.listdir(os.path.join(os.path.dirname(os.__file__), "../Textures")) if "@2x" not in name]

All "built-in" images are stored in the Textures folder inside the Pythonista.app bundle. Due to recent changes in iOS 8 it is no longer possible to easily access that location, so we join the path of the os module source (which we need imported anyway) with a relative path to get the path to the Textures folder. Then we os.listdir the folder and use a list comprehension to remove any "retina" versions of already listed images from the list.

As for sounds, those are stored in the root of the Pythonista.app. I think they are all in CAF format, so it would probably be possible to os.listdir the Pythonista.app folder and filter out everything that does not end with .caf.

dgelessus

@TutorialDoctor, soon (when 1.6 is released) that will no longer be possible. The Pythonista.app bundle has been further "sandboxed away" from the Documents folder in iOS 8, but that change won't become obvious until the app is updated for iOS 8. (I know already, because I have access to the beta version of 1.6.) Maybe I'll add a few convenient buttons on the filenav window to change between the ~ and Pythonista.app folders.

bortels38

@dgelessus - awesome, exactly what I was looking for; explanation plus why I was having issues (I was only seeing my own stuff using regular old os.walk - now I know why).

@TutorialDoctor - thank you as well, that looks like a handy utility, and it's useful to look at working code, even if Apple is a bit of a moving target!

zencuke

My trick is to download the xCode snapshot then use the UNIX find command to look for the builtin files. I have the snapshot anyway because I want to build apps I can run in parallel with Pythionista.

bortels38

Heh, would be overkill for me - I was showing my daughters how if you write the code, you can modify the game to make it what you want, and suggested they could, for example, change the pictures in the "Cards.py" sample.

FWIW - I replaced the "images =" line with:

    # pick random images
    builtin_images = [name for name in os.listdir(os.path.join(os.path.dirname(os.__file__), "../Textures")) if "@2x" not in name]
    images = random.sample(builtin_images, 8)
    images = [os.path.splitext(x)[0] for x in images]
    images = images * 2

And added "random" and "os" to the imports, and there was much rejoicing. :-)

(I'm not sure os.path.splitext was the best choice, but MEH it works)