Fixed BUG-19 - video thumbs being out of ratio, using THUMBSIZE in shared.py to also make sure images and videos have at least one of their width / height be 256 in their thumbnail

This commit is contained in:
2021-01-30 20:16:36 +11:00
parent b296d08c28
commit 98d4c7d43e
3 changed files with 19 additions and 13 deletions

View File

@@ -25,7 +25,7 @@ from sqlalchemy.orm import scoped_session
### LOCAL FILE IMPORTS ###
from shared import DB_URL, PA_JOB_MANAGER_HOST, PA_JOB_MANAGER_PORT
from shared import DB_URL, PA_JOB_MANAGER_HOST, PA_JOB_MANAGER_PORT, THUMBSIZE
from datetime import datetime, timedelta, date
import pytz
import time
@@ -750,7 +750,7 @@ def GenImageThumbnail(job, file):
try:
im_orig = Image.open(file)
im = ImageOps.exif_transpose(im_orig)
im.thumbnail((256,256))
im.thumbnail((THUMBSIZE,THUMBSIZE))
img_bytearray = io.BytesIO()
im.save(img_bytearray, format='JPEG')
img_bytearray = img_bytearray.getvalue()
@@ -766,11 +766,21 @@ def GenVideoThumbnail(job, file):
AddLogForJob( job, "Generate Thumbnail from Video file: {}".format( file ), file )
try:
vcap = cv2.VideoCapture(file)
res, im_ar = vcap.read()
while im_ar.mean() < 15 and res:
res, im_ar = vcap.read()
im_ar = cv2.resize(im_ar, (160, 90), 0, 0, cv2.INTER_LINEAR)
res, thumb_buf = cv2.imencode('.jpeg', im_ar)
res, frame = vcap.read()
# if the mean pixel value is > 15, we have something worth making a sshot of (no black frame at start being the sshot)
while frame.mean() < 15 and res:
res, frame = vcap.read()
w = vcap.get(cv2.cv2.CAP_PROP_FRAME_WIDTH)
h = vcap.get(cv2.cv2.CAP_PROP_FRAME_HEIGHT)
if w > h:
factor = w / THUMBSIZE
else:
factor = h / THUMBSIZE
new_h = int(h / factor)
new_w = int(w / factor)
frame = cv2.resize(frame, (new_w, new_h), 0, 0, cv2.INTER_LINEAR)
res, thumb_buf = cv2.imencode('.jpeg', frame)
bt = thumb_buf.tobytes()
thumbnail = base64.b64encode(bt)
thumbnail = str(thumbnail)[2:-1]