/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import os import random class localImages(object): def __init__(self, directory='/home/Foto'): self.directoryScan = directory # Cartella da cui cominciare a prelevare le foto self.directoryRemain = [self.directoryScan] self.filePath = {} def getRandomImages(self): ''' Prelieva dal proprio disco delle immagini in maniera random... ''' for i in range(5): # Scansiona 5 directory if len(self.directoryRemain) > 0: directory = random.choice(self.directoryRemain) self.directoryRemain.remove(directory) files = [] try: files = os.listdir(directory) except: pass # Serve per aumentare il Random delle Foto # FIXME: cercare di aumentare questo random if len(files) > 0: for i in range(len(files)/2): files_remove = random.choice(files) files.remove(files_remove) ########################################## for filename in files: filepath = os.path.join(directory, filename) if os.path.isdir(filepath): self.directoryRemain.append(filepath) elif os.path.isfile(filepath): (name, ext) = os.path.splitext(filepath) if ext.lower() in ('.jpg', '.gif'): self.filePath[filepath] = 0 if len(self.directoryRemain) == 0: self.directoryRemain = [self.directoryScan] def downloadImages(self): ''' Scarica nella cartella localIMGs le foto che vengono trovate nel disco... ''' numberIMGs = len(self.filePath) posIMGs = 1 for imageName in self.filePath: print '[' + str(posIMGs) + '/' + str(numberIMGs) + '] - ' + imageName fread = open(imageName, 'rb') fwrite = open('localIMGs' + os.sep + os.path.split(imageName)[1], 'wb') fwrite.write(fread.read()) fread.close() fwrite.close() posIMGs += 1 if __name__ == '__main__': test = localImages() test.getRandomImages() test.downloadImages() print 'Finito...'