diff --git a/api/modules/functions.py b/api/modules/functions.py index 2a0a172..c57a9ca 100644 --- a/api/modules/functions.py +++ b/api/modules/functions.py @@ -1,20 +1,47 @@ -from io import BytesIO -from PIL import Image +import cv2 import base64 # Reducing the size of an artwork/image (PNG to JPEG) and base64-encode it. # imagepath = full file image path # expectedsize = medium or thumbnail -def reduceart(imagepath, expectedsize): - image = Image.open(imagepath) - rgb_image = image.convert("RGB") - with BytesIO() as f: - rgb_image.save(f, format="JPEG") - f.seek(0) +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() - if expectedsize == 'thumbnail': - rgb_image.thumbnail([90,90]) - rgb_image.save(f, format="JPEG") - f.seek(0) - return base64.b64encode(f.getvalue()).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 diff --git a/api/modules/game.py b/api/modules/game.py index 378297b..63067af 100644 --- a/api/modules/game.py +++ b/api/modules/game.py @@ -3,7 +3,7 @@ from flask import jsonify, request import base64 import json import modules.db_connect -from modules.functions import reduceart +from modules.functions import reduceartcv2 import os contentpath = modules.db_connect.contentpath() @@ -25,10 +25,10 @@ def game(predefinednfo=False, return_dict=False, skip_artwork=False): i = 0 for art in nfo['game']['artwork']: if skip_artwork is False: - nfo['game']['artwork'][i]['data'] = reduceart( + nfo['game']['artwork'][i]['data'] = reduceartcv2( artpath+art['filename'], 'thumbnail') if art['type'] == 'front': - nfo['game']['displayimage'] = reduceart( + nfo['game']['displayimage'] = reduceartcv2( artpath+art['filename'], 'thumbnail') i += 1 @@ -58,7 +58,7 @@ def artwork(size='max'): # Changes filetype to jpeg for size reduction. This also drops # Alpha-channel else: - art['img'] = reduceart(artpath+art['filename'], size) + art['img'] = reduceartcv2(artpath+art['filename'], size) artlist.append(art) diff --git a/site/modules/gamelist.py b/site/modules/gamelist.py index 06fa2a8..8ffde00 100644 --- a/site/modules/gamelist.py +++ b/site/modules/gamelist.py @@ -1,8 +1,7 @@ from __main__ import app -import datetime import json import requests -from flask import render_template, redirect, request, url_for +from flask import render_template import modules.init host_endpoint = modules.init.host_endpoint()