Resize image (because mastodon not accept big picture)

This commit is contained in:
Dryusdan 2018-08-18 00:40:21 +02:00
parent cf905aac3c
commit d9ef0dc3af

35
bot.py
View file

@ -12,8 +12,7 @@ from utils.config import get_parameter, init_log, init_mastodon
from PIL import Image from PIL import Image
from io import BytesIO from io import BytesIO
import requests, os, random, sys, time, json, logging, argparse, re import requests, os, random, sys, time, json, logging, argparse, re, shutil
config_file = "config.txt" config_file = "config.txt"
secrets_filepath = get_parameter("secrets_filepath", config_file) secrets_filepath = get_parameter("secrets_filepath", config_file)
log_filepath = get_parameter("log_filepath", config_file) log_filepath = get_parameter("log_filepath", config_file)
@ -39,25 +38,26 @@ def post_img_local(mastodon, text, log, config):
else: else:
if ".zip" not in file: if ".zip" not in file:
continu = False continu = False
#file, ext = os.path.splittext(img_path+"/"+file) im = Image.open(img_path+ file)
#if ext in mime_dict:
im = Image.open(img_path+"/"+file)
width, height = im.size width, height = im.size
NEW_WIDTH = 1980
if width > 1980: if width > 1980:
ratio_img = width / 1980; difference_percent = NEW_WIDTH / width
new_width = width / ratio_img new_height = height * difference_percent
new_height = height / ratio_img size = new_height, NEW_WIDTH
im = im.resize((int(NEW_WIDTH), int(new_height)))
im.save('resize_img.jpg')
file = "resize_img.jpg"
shutil.copyfile(file, "/tmp/"+file)
else:
log.debug("no resize")
shutil.copyfile(img_path+file, "/tmp/"+file)
image = im.resize((int(new_width), int(new_height))) image_byte = open("/tmp/"+file, "rb").read()
im.save(img_path+"/"+file) file, ext = os.path.splitext(file)
os.remove("/tmp/"+file+ext)
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: try:
mime = mime_dict[str.lower(ext)] mime = mime_dict[str.lower(ext)]
except KeyError: except KeyError:
@ -68,7 +68,6 @@ def post_img_local(mastodon, text, log, config):
media_dict = mastodon.media_post(image_byte, mime) media_dict = mastodon.media_post(image_byte, mime)
return media_dict; return media_dict;
def post_img_distant(mastodon, text, log, config): def post_img_distant(mastodon, text, log, config):
collection_url = get_parameter("collection_url", config) collection_url = get_parameter("collection_url", config)