Resize image (because mastodon not accept big picture)
This commit is contained in:
parent
cf905aac3c
commit
d9ef0dc3af
35
bot.py
35
bot.py
|
@ -12,8 +12,7 @@ from utils.config import get_parameter, init_log, init_mastodon
|
|||
from PIL import Image
|
||||
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"
|
||||
secrets_filepath = get_parameter("secrets_filepath", config_file)
|
||||
log_filepath = get_parameter("log_filepath", config_file)
|
||||
|
@ -39,25 +38,26 @@ def post_img_local(mastodon, text, log, config):
|
|||
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)
|
||||
|
||||
im = Image.open(img_path+ file)
|
||||
width, height = im.size
|
||||
NEW_WIDTH = 1980
|
||||
if width > 1980:
|
||||
ratio_img = width / 1980;
|
||||
new_width = width / ratio_img
|
||||
new_height = height / ratio_img
|
||||
difference_percent = NEW_WIDTH / width
|
||||
new_height = height * difference_percent
|
||||
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)))
|
||||
im.save(img_path+"/"+file)
|
||||
image_byte = open("/tmp/"+file, "rb").read()
|
||||
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:
|
||||
mime = mime_dict[str.lower(ext)]
|
||||
except KeyError:
|
||||
|
@ -68,7 +68,6 @@ def post_img_local(mastodon, text, log, config):
|
|||
media_dict = mastodon.media_post(image_byte, mime)
|
||||
return media_dict;
|
||||
|
||||
|
||||
def post_img_distant(mastodon, text, log, config):
|
||||
|
||||
collection_url = get_parameter("collection_url", config)
|
||||
|
|
Reference in New Issue