Created
February 25, 2016 02:20
-
-
Save nbadal/7757ee08130c572246f7 to your computer and use it in GitHub Desktop.
Generates images with random dates
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
#!/usr/bin/python | |
from PIL import Image, ImageFont, ImageDraw, ExifTags | |
import piexif | |
from datetime import datetime, timedelta | |
import random | |
import sys | |
def random_date(start, end): | |
delta = end - start | |
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds | |
random_second = random.randrange(int_delta) | |
return start + timedelta(seconds=random_second) | |
def generateImages(count, maxagedays): | |
today = datetime.now() | |
mindate = today - timedelta(days=int(maxagedays)) | |
for x in xrange(int(count)): | |
r = random.randrange(0,255) | |
g = random.randrange(0,255) | |
b = random.randrange(0,255) | |
# Random color rectangle | |
im = Image.new('RGBA', (140, 140), (255, 255, 255, 0)) | |
draw = ImageDraw.Draw(im) | |
fill = (r, g, b, 0xff) | |
outline = (0,0,0,255) | |
draw.rectangle((0, 0, 140, 140), fill=fill, outline=outline) | |
# Generate random date | |
datestr = random_date(mindate, today).strftime('%Y:%m:%d %H:%M:%S') | |
print "Random date is "+datestr | |
filename = ("fake_%s.jpeg" % datestr).replace(":", "-") | |
im.save(filename, "jpeg") | |
# Exif data | |
exif_dict = piexif.load(filename) | |
exif_dict["Exif"][piexif.ExifIFD.DateTimeOriginal] = datestr | |
exif_bytes = piexif.dump(exif_dict) | |
piexif.insert(exif_bytes, filename) | |
if __name__ == '__main__': | |
if len(sys.argv) < 2 or len(sys.argv) > 3: | |
print 'Usage: ', sys.argv[0], ' <count> [<maxagedays>]' | |
exit(1) | |
maxagedays = 30 if len(sys.argv) < 3 else sys.argv[2] | |
generateImages(sys.argv[1], maxagedays) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment