Last active
December 29, 2015 17:49
-
-
Save latenssi/7707041 to your computer and use it in GitHub Desktop.
Django lorem pixels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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