Skip to content

Instantly share code, notes, and snippets.

@slavakurilyak
Created November 10, 2016 20:20
Show Gist options
  • Save slavakurilyak/d3418446179f98cde5b7d6b53eefa09d to your computer and use it in GitHub Desktop.
Save slavakurilyak/d3418446179f98cde5b7d6b53eefa09d to your computer and use it in GitHub Desktop.
Get random image from a folder

Get random image from a folder

Dealing with a image dataset? Dealing with large folder of jpg? Use this script to print a random image from a folder.

Usage

To print a randomly selected image, type:

$ python random-image-from-folder.py <path>

To print two randomly selected images, type:

$ chmod u+x two-random-images.sh
$ ./two-random-images.sh <path>

Examples

$ python download-images-from-csv.py fullres

Assuming fullres is a folder with JPGs

$ python download-images-from-csv.py thumbnails

Assuming thumbnails is a folder with JPGs

$ chmod u+x two-random-images.sh
$ ./two-random-images.sh thumbnails

Results

Images are selected randomly, printed as <filename>

Inspired By

import sys
import os
import random
path = sys.argv[1]
def randomize_file():
return random.choice(os.listdir(path))
def print_random_filename(random_file):
random_filename = random_file.split(".jpg")[0]
print random_filename
random_file = random.choice(os.listdir(path))
if random_file.startswith('.'):
new_random_file = randomize_file()
print_random_filename(new_random_file)
else:
print_random_filename(random_file)
#!/bin/sh
python random-image-from-folder.py $1
python random-image-from-folder.py $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment