# (Python?) How to get the image editor to open a random image from a folder

I'm wondering if there is a way, I'm guessing a little python script, to get the image editor to open a random image from a folder on my drive. I saw a similar thread, but it wasn't about opening one random image.

I'm on mac if that is of any interest.

os.listdir() will give you a list of the files in a directory.

random.choice() will pick a random item in a list.

Once you have the random file name you can then open it in the UV/Image Editor.

import bpy, random, os

imgdir = '/path/to/images'

imgfile = random.choice(os.listdir(imgdir))

bpy.ops.image.open(filepath=os.path.join(imgdir, imgfile))

new_image = bpy.data.images[imgfile]

# show the new image in the image editor
for area in bpy.context.screen.areas :
if area.type == 'IMAGE_EDITOR' :
area.spaces.active.image = new_image


Wait so do you mean like open a random texture for a game? Well you would have to make a random sensor (switch it to int uniform) and then make the min 1, and the max however many pictures you have. Then add a property sensor that is equals to 1, 2 3 and so on so that you have lots of equals sensors. They all each make a picture get added in. I know this isn't from a drive, but for a drive it wouldn't really work because it would have to make a new UV map and a new material for every time you wanted a texture and that simply isn't something blender can do on its own from what I have heard. Their might be a way, but I certainly don't know about it.

• This is kind of an example of what I mean, but instead of adding chunks of city on multiple empties, it adds a random texture on a single empty. youtube.com/watch?v=YcUj_yaYkeY
– user37321
Mar 25, 2017 at 20:16