Offload image processing to the Plex server

This commit is contained in:
JonnyWong16 2018-03-17 14:03:27 -07:00
parent 003e890844
commit 1f7be7a4d5
10 changed files with 96 additions and 158 deletions

View file

@ -26,12 +26,6 @@ from string import Formatter
import threading
import time
try:
from PIL import Image, ImageFilter
PILLOW = True
except ImportError:
PILLOW = False
import plexpy
import activity_processor
import common
@ -1071,53 +1065,42 @@ def format_group_index(group_keys):
return ','.join(num) or '0', ','.join(num00) or '00'
def get_poster_info(poster_thumb='', poster_key='', poster_title='', art=False, width='', height='', blur=False):
def get_poster_info(poster_thumb='', poster_key='', poster_title='', art=False,
width='', height='', opacity=None, background=None, blur=None):
default_poster_info = {'poster_title': '', 'poster_url': ''}
default_art_info = {'art_title': '', 'art_url': ''}
# Try to retrieve poster info from the database
data_factory = datafactory.DataFactory()
poster_info = data_factory.get_poster_info(rating_key=poster_key, art=art)
poster_info = data_factory.get_poster_info(rating_key=poster_key, art=art, blur=blur)
# If no previous poster info
if not poster_info and poster_thumb:
try:
thread_name = str(threading.current_thread().ident)
poster_file = os.path.join(plexpy.CONFIG.CACHE_DIR, 'cache-image-%s.jpg' % thread_name)
poster_file = os.path.join(plexpy.CONFIG.CACHE_DIR, 'cache-image-%s.png' % thread_name)
# Retrieve the poster from Plex and cache to file
pms_connect = pmsconnect.PmsConnect()
result = pms_connect.get_image(img=poster_thumb, width=width, height=height)
result = pms_connect.get_image(img=poster_thumb,
width=width,
height=height,
opacity=opacity,
background=background,
blur=blur)
if result and result[0]:
with open(poster_file, 'wb') as f:
f.write(result[0])
else:
raise Exception(u'PMS image request failed')
if blur and PILLOW:
img = Image.open(poster_file)
img = img.convert("RGBA")
img = img.filter(ImageFilter.GaussianBlur(3)) # 3px blur
img.putalpha(64) # 40% opacity
# Save as a png
poster_file_blur = os.path.join(plexpy.CONFIG.CACHE_DIR, 'cache-image-%s.png' % thread_name)
img.save(poster_file_blur)
# Upload poster_thumb to Imgur and get link
if blur:
poster_url, delete_hash = helpers.upload_to_imgur(poster_file_blur, poster_title)
else:
poster_url, delete_hash = helpers.upload_to_imgur(poster_file, poster_title)
poster_url, delete_hash = helpers.upload_to_imgur(poster_file, poster_title)
if poster_url:
# Create poster info
if art:
poster_info = {'art_title': poster_title}
if blur:
poster_info['blur_art_url'] = poster_url
else:
poster_info['art_url'] = poster_url
poster_info = {'art_title': poster_title, 'art_url': poster_url}
else:
poster_info = {'poster_title': poster_title, 'poster_url': poster_url}
@ -1131,8 +1114,6 @@ def get_poster_info(poster_thumb='', poster_key='', poster_title='', art=False,
# Delete the cached poster
os.remove(poster_file)
if blur:
os.remove(poster_file_blur)
except Exception as e:
logger.error(u"Tautulli NotificationHandler :: Unable to retrieve poster for rating_key %s: %s."
% (poster_key, e))