bcns-gameDistributionSystem/bcnsGDSAPI/modules/functions.py
odecif 77b0bcb94a Prepare API for gunicorn
It is now possible to run the API with gunicorn using
gunicorn -w 4 -b 0.0.0.0:8001 'bcnsGDSAPI:create_app()'.

Also did other small changes.
2023-11-21 12:51:35 +01:00

77 lines
2.0 KiB
Python

import cv2
import base64
from io import BytesIO
import zipfile
import pathlib
gamelist = []
# Set the gamelist
def set_gamelist(gamelist_dict):
global gamelist
gamelist = gamelist_dict
return True
# Get the gamelist
def get_gamelist():
global gamelist
return gamelist
# Reducing the size of an artwork/image (PNG to JPEG) and base64-encode it.
# imagepath = full file image path
# expectedsize = medium or thumbnail
def reduceartcv2(imagepath, expectedsize):
if expectedsize == 'thumbnail':
image = cv2.imread(imagepath)
thumb_s = image_resize(image, width=90)
retval, thumb = cv2.imencode('.jpg', thumb_s)
return base64.b64encode(thumb).decode()
# Stolen from stackoverflow
# Resize image and keep aspect ratio
def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
# grab the image size
dim = None
(h, w) = image.shape[:2]
# if both the width and height are None, then return the
# original image
if width is None and height is None:
return image
# check to see if the width is None
if width is None:
# calculate the ratio of the height and construct the
# dimensions
r = height / float(h)
dim = (int(w * r), height)
# otherwise, the height is None
else:
# calculate the ratio of the width and construct the
# dimensions
r = width / float(w)
dim = (width, int(h * r))
# resize the image
resized = cv2.resize(image, dim, interpolation=inter)
# return the resized image
return resized
# Create a ZIP-file on the fly for downloading games quicker
def make_archive(dirname, path):
base_path = pathlib.Path(path)
b = BytesIO()
with zipfile.ZipFile(b, mode="w", compression=zipfile.ZIP_DEFLATED) as z:
for f_name in base_path.iterdir():
z.write(f_name, arcname=f_name.name)
b.seek(0)
return b