Moved combineImg to own module; created AWS dockerfile; added combination_method config

This commit is contained in:
Dylan 2022-05-25 23:48:55 +01:00
parent d7e0cb9089
commit 022edb5122
3 changed files with 63 additions and 2 deletions

33
combineImg/Dockerfile Normal file
View file

@ -0,0 +1,33 @@
FROM public.ecr.aws/lambda/python:3.8
RUN yum -y install git && yum clean all
RUN yum -y install tar gzip zlib freetype-devel \
gcc \
ghostscript \
lcms2-devel \
libffi-devel \
libimagequant-devel \
libjpeg-devel \
libraqm-devel \
libtiff-devel \
libwebp-devel \
make \
openjpeg2-devel \
rh-python36 \
rh-python36-python-virtualenv \
sudo \
tcl-devel \
tk-devel \
tkinter \
which \
xorg-x11-server-Xvfb \
zlib-devel \
&& yum clean all
RUN pip install -U --force-reinstall pillow-simd
RUN pip install requests
# Copy function code
COPY __init__.py ${LAMBDA_TASK_ROOT}/app.py
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.lambda_handler" ]

View file

@ -1,6 +1,10 @@
import json
from weakref import finalize
from PIL import Image, ImageOps, ImageFilter from PIL import Image, ImageOps, ImageFilter
import requests import requests
from io import BytesIO from io import BytesIO
import io
import base64
# find the highest res image in an array of images # find the highest res image in an array of images
def findImageWithMostPixels(imageArray): def findImageWithMostPixels(imageArray):
@ -83,6 +87,7 @@ def genImage(imageArray):
combinedBG = blurImage(combinedBG,50) combinedBG = blurImage(combinedBG,50)
finalImg = Image.alpha_composite(combinedBG,combined) finalImg = Image.alpha_composite(combinedBG,combined)
finalImg = ImageOps.pad(finalImg, findImageWithMostPixels(imageArray).size,color=(0, 0, 0, 0)) finalImg = ImageOps.pad(finalImg, findImageWithMostPixels(imageArray).size,color=(0, 0, 0, 0))
finalImg = finalImg.convert('RGB')
return finalImg return finalImg
def genImageFromURL(urlArray): def genImageFromURL(urlArray):
@ -93,3 +98,20 @@ def genImageFromURL(urlArray):
for url in urlArray: for url in urlArray:
imageArray.append(Image.open(BytesIO(requests.get(url).content))) imageArray.append(Image.open(BytesIO(requests.get(url).content)))
return genImage(imageArray) return genImage(imageArray)
def lambda_handler(event, context):
# TODO implement
images = event["queryStringParameters"].get("imgs","").split(",")
combined = genImageFromURL(images)
buffered = BytesIO()
combined.save(buffered,format="JPEG")
combined_str=base64.b64encode(buffered.getvalue()).decode('ascii')
return {
'statusCode': 200,
"headers":
{
"Content-Type": "image/jpg"
},
'body': combined_str,
'isBase64Encoded': True
}

View file

@ -46,7 +46,8 @@ if not os.path.exists("config.json"):
"color":"#43B581", "color":"#43B581",
"appname": "vxTwitter", "appname": "vxTwitter",
"repo": "https://github.com/dylanpdx/BetterTwitFix", "repo": "https://github.com/dylanpdx/BetterTwitFix",
"url": "https://vxtwitter.com" "url": "https://vxtwitter.com",
"combination_method": "local" # can either be 'local' or a URL to a server handling requests in the same format
}, },
"api":{"api_key":"[api_key goes here]", "api":{"api_key":"[api_key goes here]",
"api_secret":"[api_secret goes here]", "api_secret":"[api_secret goes here]",
@ -222,6 +223,11 @@ def favicon():
def rendercombined(): def rendercombined():
# get "imgs" from request arguments # get "imgs" from request arguments
imgs = request.args.get("imgs", "") imgs = request.args.get("imgs", "")
if 'combination_method' in config['config'] and config['config']['combination_method'] != "local":
return redirect(config['config']['combination_method']+"?imgs="+imgs, 301)
# Redirecting here instead of setting the embed URL directly to this because if the config combination_method changes in the future, old URLs will still work
imgs = imgs.split(",") imgs = imgs.split(",")
if (len(imgs) == 0 or len(imgs)>4): if (len(imgs) == 0 or len(imgs)>4):
abort(400) abort(400)