Skip to content

Instantly share code, notes, and snippets.

@latenssi
Last active December 29, 2015 17:49
Show Gist options
  • Save latenssi/7707041 to your computer and use it in GitHub Desktop.
Save latenssi/7707041 to your computer and use it in GitHub Desktop.
Django lorem pixels
import os,urllib
from random import choice
from django.conf import settings
TYPES = [
'abstract',
'city',
'people',
'transport',
'animals',
'food',
'nature',
'business',
'nightlife',
'sports',
'cats',
'fashion',
'technics'
]
def get_images(width=256, height=256, count=10, types=TYPES):
images = []
image_folder = os.path.join(getattr(settings, 'MEDIA_ROOT'))
for x in xrange(count):
image_name = "{0}-{1}-{2}".format(width, height, choice(types))
image = {
'url': 'http://lorempixel.com/%s/' % image_name.replace('-', '/'),
'width': width,
'height': height,
'filepath': os.path.join('lorem_pixels', image_name+'.jpg')
}
full_path = os.path.join(image_folder, image['filepath'])
if not os.path.exists(full_path):
urllib.urlretrieve(image['url'], full_path)
images.append(image)
return images
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment