From a5633e5c18cbe9c5975edc22813627d5ffbb973c Mon Sep 17 00:00:00 2001 From: Dryusdan Date: Sat, 23 Jun 2018 00:58:57 +0200 Subject: [PATCH] select image recursive folder #4 --- bot.py | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/bot.py b/bot.py index 1db33e6..b81bbb8 100755 --- a/bot.py +++ b/bot.py @@ -29,31 +29,45 @@ blacklist_file.close() mime_dict = {'.jpg': 'image/jpeg', '.jpe': 'image/jpeg', '.jpeg': 'image/jpeg', '.png': 'image/png', '.gif': 'image/gif'} def post_img_local(mastodon, text, log, config): - img_path = get_parameter("img_path", config) + img_path = get_parameter("img_path", config) - continu = True; - while continu: - file = random.choice(os.listdir(img_path+"/")) - if os.path.isdir(file): - img_path = img_path+"/"+file - else: - continu = False - - #file = random.choice(os.listdir(img_path+"/")) - image_byte = open(img_path+"/"+file, "rb").read() + continu = True; + while continu: + file = random.choice(os.listdir(img_path+"/")) + if os.path.isdir(img_path+file): + img_path = img_path+file+"/" + else: + if ".zip" not in file: + continu = False + + #file, ext = os.path.splittext(img_path+"/"+file) + #if ext in mime_dict: + im = Image.open(img_path+"/"+file) + width, height = im.size + if width > 1980: + ratio_img = width / 1980; + new_width = width / ratio_img + new_height = height / ratio_img - file, ext = os.path.splitext(file) + image = im.resize((int(new_width), int(new_height))) + im.save(img_path+"/"+file) + + im.close() + + image_byte = open(img_path+"/"+file, "rb").read() + file, ext = os.path.splitext(img_path+"/"+file) #mime = mime_dict[str.lower(ext)] - try: - mime = mime_dict[str.lower(ext)] - except KeyError: - mime = None; - log.error(ext + " is not present on mime_dict, please add this") - pass + try: + mime = mime_dict[str.lower(ext)] + except KeyError: + mime = None; + log.error(ext + " is not present on mime_dict, please add this") + pass + + media_dict = mastodon.media_post(image_byte, mime) + return media_dict; - media_dict = mastodon.media_post(image_byte, mime) - return media_dict; def post_img_distant(mastodon, text, log, config):