5-replace-PIL-with-cv2 #6
@ -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
|
||||||
|
```
|
||||||
|
|||||||
@ -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)
|
if expectedsize == 'thumbnail':
|
||||||
rgb_image = image.convert("RGB")
|
image = cv2.imread(imagepath)
|
||||||
with BytesIO() as f:
|
thumb_s = image_resize(image, width=90)
|
||||||
rgb_image.save(f, format="JPEG")
|
retval, thumb = cv2.imencode('.jpg', thumb_s)
|
||||||
f.seek(0)
|
return base64.b64encode(thumb).decode()
|
||||||
|
|
||||||
if expectedsize == 'thumbnail':
|
|
||||||
rgb_image.thumbnail([90,90])
|
# Stolen from stackoverflow
|
||||||
rgb_image.save(f, format="JPEG")
|
# Resize image and keep aspect ratio
|
||||||
f.seek(0)
|
def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
|
||||||
return base64.b64encode(f.getvalue()).decode()
|
# 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
|
||||||
|
|||||||
@ -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)
|
||||||
|
|
||||||
|
|||||||
@ -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()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user