From 022edb5122ddef8a8b3f84ffe84b1b2e4cf8130f Mon Sep 17 00:00:00 2001 From: Dylan Date: Wed, 25 May 2022 23:48:55 +0100 Subject: [PATCH] Moved combineImg to own module; created AWS dockerfile; added combination_method config --- combineImg/Dockerfile | 33 +++++++++++++++++++++++++ combineImg.py => combineImg/__init__.py | 24 +++++++++++++++++- twitfix.py | 8 +++++- 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 combineImg/Dockerfile rename combineImg.py => combineImg/__init__.py (86%) diff --git a/combineImg/Dockerfile b/combineImg/Dockerfile new file mode 100644 index 0000000..331aed2 --- /dev/null +++ b/combineImg/Dockerfile @@ -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" ] \ No newline at end of file diff --git a/combineImg.py b/combineImg/__init__.py similarity index 86% rename from combineImg.py rename to combineImg/__init__.py index d9181ab..edd85ba 100644 --- a/combineImg.py +++ b/combineImg/__init__.py @@ -1,6 +1,10 @@ +import json +from weakref import finalize from PIL import Image, ImageOps, ImageFilter import requests from io import BytesIO +import io +import base64 # find the highest res image in an array of images def findImageWithMostPixels(imageArray): @@ -83,6 +87,7 @@ def genImage(imageArray): combinedBG = blurImage(combinedBG,50) finalImg = Image.alpha_composite(combinedBG,combined) finalImg = ImageOps.pad(finalImg, findImageWithMostPixels(imageArray).size,color=(0, 0, 0, 0)) + finalImg = finalImg.convert('RGB') return finalImg def genImageFromURL(urlArray): @@ -92,4 +97,21 @@ def genImageFromURL(urlArray): imageArray = [] for url in urlArray: imageArray.append(Image.open(BytesIO(requests.get(url).content))) - return genImage(imageArray) \ No newline at end of file + 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 + } \ No newline at end of file diff --git a/twitfix.py b/twitfix.py index 81f3d7e..26138dc 100644 --- a/twitfix.py +++ b/twitfix.py @@ -46,7 +46,8 @@ if not os.path.exists("config.json"): "color":"#43B581", "appname": "vxTwitter", "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_secret":"[api_secret goes here]", @@ -222,6 +223,11 @@ def favicon(): def rendercombined(): # get "imgs" from request arguments 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(",") if (len(imgs) == 0 or len(imgs)>4): abort(400)