2017-08-17 10:44:31 +00:00
|
|
|
#!/usr/bin/python3
|
2010-10-16 11:49:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2010-11-30 17:29:33 +00:00
|
|
|
from __future__ import division
|
|
|
|
import os
|
|
|
|
|
2011-11-03 12:32:23 +00:00
|
|
|
root_dir = os.path.normpath(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
2010-11-30 17:23:49 +00:00
|
|
|
|
2013-02-19 09:11:52 +00:00
|
|
|
# using virtualenv's activate_this.py to reorder sys.path
|
2016-08-23 10:27:06 +00:00
|
|
|
activate_this = os.path.join(root_dir, 'bin', 'activate_this.py')
|
|
|
|
if os.path.exists(activate_this):
|
|
|
|
with open(activate_this) as f:
|
|
|
|
code = compile(f.read(), activate_this, 'exec')
|
|
|
|
exec(code, dict(__file__=activate_this))
|
2010-11-30 17:23:49 +00:00
|
|
|
|
2016-06-25 18:36:20 +00:00
|
|
|
from PIL import Image
|
|
|
|
from PIL import ImageDraw
|
2013-06-06 11:54:01 +00:00
|
|
|
import json
|
2010-10-16 11:49:45 +00:00
|
|
|
from optparse import OptionParser
|
2013-06-06 11:54:01 +00:00
|
|
|
import ox
|
2013-06-06 16:21:49 +00:00
|
|
|
from ox.image import drawText, getRGB, getTextSize, wrapText
|
2013-06-26 12:14:10 +00:00
|
|
|
import subprocess
|
2010-10-16 11:49:45 +00:00
|
|
|
import sys
|
|
|
|
|
2011-10-27 22:15:37 +00:00
|
|
|
static_root = os.path.join(os.path.dirname(__file__), 'data')
|
2010-10-16 11:49:45 +00:00
|
|
|
|
2013-06-26 12:14:10 +00:00
|
|
|
def get_frame(id, height, position):
|
|
|
|
cmd = [
|
|
|
|
os.path.join(root_dir, 'pandora', 'manage.py'), 'get_frame',
|
|
|
|
str(id), str(height), "%f" % (round(position * 25) / 25)
|
|
|
|
]
|
|
|
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
|
|
|
|
output = p.communicate()
|
|
|
|
frame_path = output[0].strip()
|
|
|
|
if frame_path:
|
|
|
|
frame_image = Image.open(frame_path)
|
|
|
|
return frame_image
|
|
|
|
|
2013-06-06 11:54:01 +00:00
|
|
|
def render_poster(data, poster):
|
|
|
|
|
2013-07-21 10:42:19 +00:00
|
|
|
title = ox.decode_html(data.get('title', '')).upper()
|
|
|
|
director = ox.decode_html(u', '.join(data.get('director', []))).upper()
|
2017-10-15 09:57:42 +00:00
|
|
|
for key, value in {u'\u03a0': 'PI', u'ß': u'SS'}.items():
|
2013-07-21 10:42:19 +00:00
|
|
|
title = title.replace(key, value)
|
|
|
|
director = director.replace(key, value)
|
2013-06-06 11:54:01 +00:00
|
|
|
year = str(data.get('year', ''))
|
2013-06-06 16:21:49 +00:00
|
|
|
duration = data.get('duration')
|
2013-06-06 11:54:01 +00:00
|
|
|
oxdb_id = data['oxdbId']
|
2013-06-06 20:01:49 +00:00
|
|
|
id = data['id']
|
2013-06-06 12:08:10 +00:00
|
|
|
frame = data.get('frame')
|
|
|
|
timeline = data.get('timeline')
|
2013-06-06 16:21:49 +00:00
|
|
|
|
|
|
|
poster_size = (640, 1024)
|
|
|
|
frame_size = (640, 480)
|
|
|
|
frame_ratio = frame_size[0] / frame_size[1]
|
|
|
|
logo_size = (64, 32)
|
|
|
|
small_frames = 8
|
|
|
|
small_frame_size = (80, 64)
|
|
|
|
small_frame_ratio = small_frame_size[0] / small_frame_size[1]
|
2013-06-29 15:16:27 +00:00
|
|
|
timeline_size = (640, 32)
|
2013-06-06 16:21:49 +00:00
|
|
|
margin = 16
|
|
|
|
font_size_small = 32
|
|
|
|
font_size_large = 48
|
2013-06-06 20:01:49 +00:00
|
|
|
font_file = os.path.join(static_root, 'MontserratBold.ttf')
|
2013-06-06 16:21:49 +00:00
|
|
|
hue = int(oxdb_id[2:10], 16) / pow(2, 32) * 360
|
2013-06-06 19:45:28 +00:00
|
|
|
image_color = getRGB([hue, 1, 0.2])
|
|
|
|
background_color = getRGB([hue, 1, 0.4])
|
|
|
|
foreground_color = getRGB([hue, 1, 0.8])
|
2013-06-06 16:21:49 +00:00
|
|
|
poster_image = Image.new('RGB', poster_size)
|
2010-10-16 11:49:45 +00:00
|
|
|
draw = ImageDraw.Draw(poster_image)
|
2010-11-18 17:33:46 +00:00
|
|
|
|
2010-10-16 11:49:45 +00:00
|
|
|
# frame
|
|
|
|
if frame:
|
|
|
|
frame_image = Image.open(frame)
|
|
|
|
frame_image_ratio = frame_image.size[0] / frame_image.size[1]
|
|
|
|
if frame_ratio < frame_image_ratio:
|
2013-06-06 16:21:49 +00:00
|
|
|
frame_image = frame_image.resize((int(frame_size[1] * frame_image_ratio), frame_size[1]), Image.ANTIALIAS)
|
|
|
|
left = int((frame_image.size[0] - frame_size[0]) / 2)
|
|
|
|
frame_image = frame_image.crop((left, 0, left + frame_size[0], frame_size[1]))
|
2010-10-16 11:49:45 +00:00
|
|
|
else:
|
2013-06-06 16:21:49 +00:00
|
|
|
frame_image = frame_image.resize((frame_size[0], int(frame_size[0] / frame_image_ratio)), Image.ANTIALIAS)
|
2013-06-25 10:07:01 +00:00
|
|
|
top = int((frame_image.size[1] - frame_size[1]) / 2)
|
2013-06-06 16:21:49 +00:00
|
|
|
frame_image = frame_image.crop((0, top, frame_size[0], top + frame_size[1]))
|
2010-10-16 11:49:45 +00:00
|
|
|
poster_image.paste(frame_image, (0, 0))
|
2013-06-06 19:45:28 +00:00
|
|
|
else:
|
|
|
|
draw.rectangle(((0, 0), frame_size), fill=image_color)
|
2010-10-16 11:49:45 +00:00
|
|
|
|
2013-06-06 16:21:49 +00:00
|
|
|
# logo
|
2013-06-29 14:49:34 +00:00
|
|
|
logo_image = Image.open(os.path.join(static_root, 'logo.0xdb.png'))
|
2013-06-06 16:21:49 +00:00
|
|
|
logo_image = logo_image.resize(logo_size, Image.ANTIALIAS)
|
|
|
|
for y in range(logo_size[1]):
|
|
|
|
for x in range(logo_size[0]):
|
|
|
|
poster_color = poster_image.getpixel((margin + x, margin + y))
|
|
|
|
logo_lightness = logo_image.getpixel((x, y))[0] / 255
|
|
|
|
logo_alpha = logo_image.getpixel((x, y))[3] / 255
|
|
|
|
logo_color = getRGB([hue, 1, logo_lightness])
|
|
|
|
color = tuple([int(logo_color[i] * logo_alpha + poster_color[i] * (1 - logo_alpha)) for i in range(3)])
|
|
|
|
poster_image.putpixel((margin + x, margin + y), color)
|
|
|
|
|
|
|
|
# small frames
|
2013-06-06 17:36:49 +00:00
|
|
|
if duration:
|
|
|
|
for i in range(small_frames):
|
|
|
|
position = duration * (i + 1) / (small_frames + 1)
|
2013-06-26 12:14:10 +00:00
|
|
|
small_frame_image = get_frame(id, 96, round(position * 25) / 25)
|
|
|
|
if small_frame_image:
|
|
|
|
small_frame_image_ratio = small_frame_image.size[0] / small_frame_image.size[1]
|
|
|
|
if small_frame_ratio < small_frame_image_ratio:
|
|
|
|
small_frame_image = small_frame_image.resize((int(small_frame_size[1] * small_frame_image_ratio), small_frame_size[1]), Image.ANTIALIAS)
|
|
|
|
left = int((small_frame_image.size[0] - small_frame_size[0]) / 2)
|
|
|
|
small_frame_image = small_frame_image.crop((left, 0, left + small_frame_size[0], small_frame_size[1]))
|
|
|
|
else:
|
|
|
|
small_frame_image = small_frame_image.resize((small_frame_size[0], int(small_frame_size[0] / small_frame_image_ratio)), Image.ANTIALIAS)
|
|
|
|
top = int((small_frame_image.size[1] - small_frame_size[1]) / 2)
|
|
|
|
small_frame_image = small_frame_image.crop((0, top, small_frame_size[0], top + small_frame_size[1]))
|
|
|
|
poster_image.paste(small_frame_image, (i * small_frame_size[0], frame_size[1]))
|
2013-06-06 19:45:28 +00:00
|
|
|
else:
|
|
|
|
draw.rectangle(((0, frame_size[1]), (poster_size[0], frame_size[1] + small_frame_size[1])), fill=image_color)
|
2010-10-16 11:49:45 +00:00
|
|
|
|
|
|
|
# text
|
2013-06-06 19:45:28 +00:00
|
|
|
draw.rectangle(((0, frame_size[1] + small_frame_size[1]), (poster_size[0], poster_size[1] - timeline_size[1])), fill=background_color)
|
2013-06-06 16:21:49 +00:00
|
|
|
offset_top = frame_size[1] + small_frame_size[1] + margin - 8
|
|
|
|
text_height = poster_size[1] - frame_size[1] - small_frame_size[1] - 3 * margin - font_size_large - timeline_size[1]
|
2010-10-16 11:49:45 +00:00
|
|
|
if not director:
|
2013-06-06 16:21:49 +00:00
|
|
|
title_max_lines = int(text_height / font_size_large)
|
2010-10-16 11:49:45 +00:00
|
|
|
else:
|
2013-06-06 16:21:49 +00:00
|
|
|
title_max_lines = min(len(wrapText(
|
|
|
|
title,
|
|
|
|
poster_size[0] - 2 * margin,
|
|
|
|
0,
|
|
|
|
font_file,
|
|
|
|
font_size_large
|
2013-06-29 15:16:27 +00:00
|
|
|
)), int((text_height - margin - font_size_small - 8) / font_size_large))
|
2013-06-06 16:21:49 +00:00
|
|
|
director_max_lines = int((text_height - title_max_lines * font_size_large) / font_size_small)
|
|
|
|
|
|
|
|
# title
|
|
|
|
lines = wrapText(
|
|
|
|
title,
|
|
|
|
poster_size[0] - 2 * margin,
|
|
|
|
title_max_lines,
|
|
|
|
font_file,
|
|
|
|
font_size_large
|
|
|
|
)
|
|
|
|
for line in lines:
|
|
|
|
drawText(
|
|
|
|
poster_image,
|
|
|
|
(margin, offset_top),
|
|
|
|
line,
|
|
|
|
font_file,
|
|
|
|
font_size_large,
|
|
|
|
foreground_color
|
|
|
|
)
|
|
|
|
offset_top += font_size_large
|
|
|
|
offset_top += margin
|
|
|
|
|
|
|
|
# director
|
2010-10-16 11:49:45 +00:00
|
|
|
if director:
|
2013-06-06 16:21:49 +00:00
|
|
|
lines = wrapText(
|
|
|
|
director,
|
|
|
|
poster_size[0] - 2 * margin,
|
|
|
|
director_max_lines,
|
|
|
|
font_file,
|
|
|
|
font_size_small
|
|
|
|
)
|
|
|
|
for line in lines:
|
|
|
|
drawText(
|
|
|
|
poster_image,
|
|
|
|
(margin, offset_top),
|
|
|
|
line,
|
|
|
|
font_file,
|
|
|
|
font_size_small,
|
|
|
|
foreground_color
|
|
|
|
)
|
|
|
|
offset_top += font_size_small
|
|
|
|
|
|
|
|
# year
|
2010-10-16 11:49:45 +00:00
|
|
|
if year:
|
2013-06-06 16:21:49 +00:00
|
|
|
drawText(
|
|
|
|
poster_image,
|
|
|
|
(margin, poster_size[1] - timeline_size[1] - margin - font_size_large),
|
|
|
|
year,
|
|
|
|
font_file,
|
|
|
|
font_size_large,
|
|
|
|
foreground_color
|
|
|
|
)
|
|
|
|
|
|
|
|
# id
|
|
|
|
drawText(
|
|
|
|
poster_image,
|
|
|
|
(
|
|
|
|
poster_size[0] - margin - getTextSize(poster_image, id, font_file, font_size_small)[0],
|
|
|
|
poster_size[1] - timeline_size[1] - margin - font_size_small
|
|
|
|
),
|
|
|
|
id,
|
|
|
|
font_file,
|
|
|
|
font_size_small,
|
|
|
|
foreground_color
|
|
|
|
)
|
2010-10-16 11:49:45 +00:00
|
|
|
|
2013-06-06 16:21:49 +00:00
|
|
|
# timeline
|
|
|
|
if timeline:
|
|
|
|
timeline_image = Image.open(timeline)
|
|
|
|
timeline_image = timeline_image.resize(timeline_size, Image.ANTIALIAS)
|
|
|
|
poster_image.paste(timeline_image, (0, poster_size[1] - timeline_size[1]))
|
2013-06-06 19:45:28 +00:00
|
|
|
else:
|
|
|
|
draw.rectangle(((0, poster_size[1] - timeline_size[1]), poster_size), fill=image_color)
|
2010-10-16 11:49:45 +00:00
|
|
|
|
|
|
|
poster_image.save(poster)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = OptionParser()
|
2013-06-06 11:54:01 +00:00
|
|
|
parser.add_option('-d', '--data', dest='data', help='json file with metadata', default=None)
|
2013-06-06 16:21:49 +00:00
|
|
|
parser.add_option('-p', '--poster', dest='poster', help='Poster (image file to be written)')
|
2010-10-16 11:49:45 +00:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2013-06-06 11:54:01 +00:00
|
|
|
if None in (options.data, options.poster):
|
2010-10-16 11:49:45 +00:00
|
|
|
parser.print_help()
|
|
|
|
sys.exit()
|
|
|
|
|
2013-06-06 11:54:01 +00:00
|
|
|
if options.data == '-':
|
|
|
|
data = json.load(sys.stdin)
|
|
|
|
else:
|
|
|
|
with open(options.data) as f:
|
|
|
|
data = json.load(f)
|
2013-06-06 12:05:15 +00:00
|
|
|
render_poster(data, options.poster)
|
2010-10-16 11:49:45 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
|