Advertisement
juliozaco

random_picture_imac.py

Feb 5th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # Name:        module1
  3. # Purpose:
  4. #
  5. # Author:      JULIO
  6. #
  7. # Created:     24/08/2014
  8. # Copyright:   (c) JULIO 2014
  9. # Licence:     <your licence>
  10. #-------------------------------------------------------------------------------
  11. #!/usr/bin/env python
  12.  
  13. import os, shutil
  14. import random, datetime, time
  15.  
  16. ext2conttype = {"jpg": "image/jpeg",
  17.                 "jpeg": "image/jpeg",
  18.                 "png": "image/png",
  19.                 "gif": "image/gif"}
  20.  
  21. def content_type(filename):
  22.     return ext2conttype[filename[filename.rfind(".")+1:].lower()]
  23.  
  24. def isimage(filename):
  25.     """true if the filename's extension is in the content-type lookup"""
  26.     filename = filename.lower()
  27.     return filename[filename.rfind(".")+1:] in ext2conttype
  28.  
  29. def random_file(dir):
  30.     """returns the filename of a randomly chosen image in dir"""
  31.     images = [f for f in os.listdir(dir) if isimage(f)]
  32.     return random.choice(images)
  33.  
  34. if __name__ == "__main__":
  35.     elegidofile = ''
  36.     images = []
  37.     ubicacion = '/Users/julio/Documents/mis_moviles/london.jpg'     # CHANGE1 : where you put the choosen picture
  38.     dir = '/Users/Shared/fotos'                                     # CHANGE2 : where you have all your pictures to choose
  39.     for (path, dirs, files) in os.walk(dir):
  40.         for name in files:
  41.             if isimage(name):
  42.                 #print name
  43.                 images.append(path + '/' +name)
  44.     if len(images)>0:
  45.         elegidofile = random.choice(images)
  46.         #print elegidofile
  47.  
  48.     if elegidofile<>'':
  49.         shutil.copy(elegidofile, ubicacion)
  50.         fcual = open('/Users/julio/Documents/06-zaprogramas/python/personal/random_picture.log','a')    # CHANGE3 : where you save your log
  51.         ahora = datetime.datetime.now().strftime("%d-%m-%Y %H:%M")        
  52.         fcual.write(ahora + chr(9)+ elegidofile+'\n')
  53.         fcual.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement