5-replace-PIL-with-cv2 #6

Merged
odecif merged 2 commits from 5-replace-PIL-with-cv2 into master 2022-10-26 17:34:52 +02:00
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. 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 ## Dependencies
```
pip install Flask pip install Flask
pip install Pillow pip install opencv-python
```

View File

@ -1,20 +1,47 @@
from io import BytesIO import cv2
from PIL import Image
import base64 import base64
# Reducing the size of an artwork/image (PNG to JPEG) and base64-encode it. # Reducing the size of an artwork/image (PNG to JPEG) and base64-encode it.
# imagepath = full file image path # imagepath = full file image path
# expectedsize = medium or thumbnail # expectedsize = medium or thumbnail
def reduceart(imagepath, expectedsize): def reduceartcv2(imagepath, expectedsize):
image = Image.open(imagepath)
rgb_image = image.convert("RGB")
with BytesIO() as f:
rgb_image.save(f, format="JPEG")
f.seek(0)
if expectedsize == 'thumbnail': if expectedsize == 'thumbnail':
rgb_image.thumbnail([90,90]) image = cv2.imread(imagepath)
rgb_image.save(f, format="JPEG") thumb_s = image_resize(image, width=90)
f.seek(0) retval, thumb = cv2.imencode('.jpg', thumb_s)
return base64.b64encode(f.getvalue()).decode() 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 base64
import json import json
import modules.db_connect import modules.db_connect
from modules.functions import reduceart from modules.functions import reduceartcv2
import os import os
contentpath = modules.db_connect.contentpath() contentpath = modules.db_connect.contentpath()
@ -25,10 +25,10 @@ def game(predefinednfo=False, return_dict=False, skip_artwork=False):
i = 0 i = 0
for art in nfo['game']['artwork']: for art in nfo['game']['artwork']:
if skip_artwork is False: if skip_artwork is False:
nfo['game']['artwork'][i]['data'] = reduceart( nfo['game']['artwork'][i]['data'] = reduceartcv2(
artpath+art['filename'], 'thumbnail') artpath+art['filename'], 'thumbnail')
if art['type'] == 'front': if art['type'] == 'front':
nfo['game']['displayimage'] = reduceart( nfo['game']['displayimage'] = reduceartcv2(
artpath+art['filename'], 'thumbnail') artpath+art['filename'], 'thumbnail')
i += 1 i += 1
@ -58,7 +58,7 @@ def artwork(size='max'):
# Changes filetype to jpeg for size reduction. This also drops # Changes filetype to jpeg for size reduction. This also drops
# Alpha-channel # Alpha-channel
else: else:
art['img'] = reduceart(artpath+art['filename'], size) art['img'] = reduceartcv2(artpath+art['filename'], size)
artlist.append(art) artlist.append(art)

View File

@ -1,8 +1,7 @@
from __main__ import app from __main__ import app
import datetime
import json import json
import requests import requests
from flask import render_template, redirect, request, url_for from flask import render_template
import modules.init import modules.init
host_endpoint = modules.init.host_endpoint() host_endpoint = modules.init.host_endpoint()