Merge pull request '5-replace-PIL-with-cv2' (#6) from 5-replace-PIL-with-cv2 into master

Reviewed-on: #6
This commit is contained in:
odecif 2022-10-26 17:34:50 +02:00
commit eaedc683e1
4 changed files with 48 additions and 21 deletions

View File

@ -3,6 +3,7 @@
A small website written in Flask with separated frontend and backend. Backend is placed on server hosting a collection of games and serves the games nfo-files to the frontend.
## Dependencies
```
pip install Flask
pip install Pillow
pip install opencv-python
```

View File

@ -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':
rgb_image.thumbnail([90,90])
rgb_image.save(f, format="JPEG")
f.seek(0)
return base64.b64encode(f.getvalue()).decode()
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

View File

@ -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)

View File

@ -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()