def update_list(directory='/mnt/photos', ACTION='init', item=''): global photolist global qiniu_photolist global extlist if ACTION == 'init': qiniu_photolist = qiniu_sync.get_list(bucket_name='reaky', prefix='IMG_') #print(qiniu_photolist) #files = os.listdir(directory) #files = glob.glob(os.path.join(directory, '*.jpg')) files = [fn for fn in os.listdir(directory) if any(fn.endswith(ext) for ext in extlist)] for item in files: make_thumb(os.path.join(directory, item)) print('init finished') elif ACTION == 'add': #if item[-3:] in extlist or item[-4:] in extlist: if any(item.endswith(ext) for ext in extlist): make_thumb(os.path.join(directory, item)) elif ACTION == 'delete': #photolist.remove(item) for photo in photolist: if photo[0] == item: print("%s delete in list.json"%item) photolist.remove(photo) base, ext = os.path.splitext(item) qiniu_sync.delete_file(bucket_name='reaky', key=item) qiniu_sync.delete_file(bucket_name='reaky', key='%s_thumb%s'%(base, ext)) break else: pass photolist.sort(reverse=True) #print(json.dumps(photolist)) fileHandle = open(os.path.join(directory, 'list.json'), 'w') fileHandle.write(json.dumps(photolist)) fileHandle.close() qiniu_sync.upload_file(bucket_name='reaky', localfile=os.path.join(directory, 'list.json'), key='list.json')
def make_thumb(path, size=200): global photolist global qiniu_photolist global extlist dirname = os.path.dirname(path) basename = os.path.basename(path) base, ext = os.path.splitext(basename) if ext[1:] not in extlist: #os.remove(path) os.rename(path, os.path.join(dirname, 'Todo', basename)) print("%s removed for not photo"%basename) return "Not photo" try: im = Image.open(path) print(basename, im.format, im.size, im.mode) exif_data = im._getexif() if exif_data is None: os.rename(path, os.path.join(os.path.dirname(path), 'Todo', basename)) print("%s removed for not photo"%basename) return "Not photo" if 306 in exif_data: #Exif.Image.DateTime exif_datetime = exif_data[306] elif 36867 in exif_data: #Exif.Photo.DateTimeOriginal exif_datetime = exif_data[36867] elif 36868 in exif_data: #Exif.Photo.DateTimeDigitized exif_datetime = exif_data[36868] else: os.rename(path, os.path.join(os.path.dirname(path), 'Todo', basename)) print("%s removed for not photo"%basename) return "Not photo" exif_datetime = exif_datetime.replace(':','').replace(' ','_') except IOError: os.rename(path, os.path.join(os.path.dirname(path), 'Todo', basename)) print("%s removed for not photo"%basename) return "Not photo" if base[0:4] != 'IMG_' or len(base) != 19 or base[12] != '_': new_path = os.path.join(dirname, 'IMG_'+exif_datetime+ext) if os.path.isfile(new_path): print("---New name %s exist!"%os.path.basename(new_path)) new_path = os.path.splitext(new_path)[0]+'-1'+ext os.rename(path, new_path) path = new_path dirname = os.path.dirname(path) basename = os.path.basename(path) base, ext = os.path.splitext(basename) print('---Rename to STD name: %s'%basename) path_thumb = os.path.join(dirname, 'thumb', '%s_thumb%s'%(base, ext)) if not os.path.isfile(path_thumb): try: im_thumb = Image.open(path) except IOError: print("%s Open IOError"%basename) return "failed" mode = im_thumb.mode width, height = im_thumb.size if mode not in ('L', 'RGB'): if mode == 'RGBA': alpha = im_thumb.split()[3] bgmask = alpha.point(lambda x: 255-x) im_thumb = im_thumb.convert('RGB') # paste(color, box, mask) im_thumb.paste((255,255,255), None, bgmask) else: im_thumb = im_thumb.convert('RGB') if width == height: region = im_thumb else: if width > height: delta = (width - height)/2 box = (delta, 0, delta+height, height) else: delta = (height - width)/2 box = (0, delta, width, delta+width) region = im_thumb.crop(box) thumb = region.resize((size,size), Image.ANTIALIAS) thumb.save(path_thumb, quality=100) print("%s Photo thumb %s"%(basename, os.path.basename(path_thumb))) #update photolist if [basename, im.size[0], im.size[1]] not in photolist: print("%s add to list.json"%basename) photolist.insert(0, [basename, im.size[0], im.size[1]]) try: fileHandle = open(os.path.join(directory, 'list.json'), 'r') old_photolist = json.loads(fileHandle.read()) fileHandle.close() if [basename, im.size[0], im.size[1]] in old_photolist: return except: print("list.json load error!") if basename not in qiniu_photolist: #print(qiniu_photolist) #print(basename) qiniu_sync.upload_file(bucket_name='reaky', localfile=path, key=basename) if os.path.basename(path_thumb) not in qiniu_photolist: qiniu_sync.upload_file(bucket_name='reaky', localfile=path_thumb, key=os.path.basename(path_thumb)) return "success"
notifier = Notifier(wm, PFilePath()) wdd = wm.add_watch(directory, mask) print(datetime.now().strftime('%Y%m%d %H%M%S: ')) print('***Start watching***') notifier.loop() #while True: # try: # notifier.process_events() # if notifier.check_events(): # notifier.read_events() # except KeyboardInterrupt: # notifier.stop() # break if __name__ == '__main__': directory='/mnt/photos' #make_thumb(r"/mnt/photos/IMG_20161003_081112-2.jpg") #qiniu_sync.upload_file(bucket_name='reaky', localfile='path', key='IMG_20150927_171146.jpg') qiniu_sync.upload_file(bucket_name='reaky', localfile='/mnt/www/Gallery/favicon.ico') qiniu_sync.upload_file(bucket_name='reaky', localfile='/mnt/www/Gallery/index.html') qiniu_sync.upload_file(bucket_name='reaky', localfile='/mnt/www/Gallery/index.js') qiniu_sync.upload_file(bucket_name='reaky', localfile='/mnt/www/Gallery/style.css') qiniu_sync.upload_file(bucket_name='reaky', localfile='/mnt/www/Gallery/jquery.uploadifive.min.js') qiniu_sync.upload_file(bucket_name='reaky', localfile='/mnt/www/Gallery/uploadifive-cancel.png') qiniu_sync.upload_file(bucket_name='reaky', localfile='/mnt/www/Gallery/uploadifive.css') #send_email(subject, body_text, emails) update_list(directory, 'init') monitor_photos(directory)