pandora/scripts/poster.indiancinema.py

221 lines
6.7 KiB
Python
Raw Permalink Normal View History

2017-11-02 21:08:17 +00:00
#!/usr/bin/env python3
2013-02-19 09:10:08 +00:00
# -*- coding: utf-8 -*-
import os
2016-06-25 18:36:20 +00:00
from PIL import Image
from PIL import ImageDraw
import json
2013-02-19 09:10:08 +00:00
from optparse import OptionParser
import ox
2013-02-19 09:10:08 +00:00
from ox.image import getRGB, drawText, wrapText
import sys
2017-11-02 21:08:17 +00:00
root_dir = os.path.normpath(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
2013-02-19 09:10:08 +00:00
static_root = os.path.join(os.path.dirname(__file__), 'data')
def render_poster(data, poster):
title = ox.decode_html(data.get('title', ''))
director = ox.decode_html(', '.join(data.get('director', [])))
year = str(data.get('year', ''))
2013-06-06 12:08:10 +00:00
frame = data.get('frame')
timeline = data.get('timeline')
2013-02-19 09:10:08 +00:00
poster_width = 704
2013-02-19 09:10:08 +00:00
poster_height = 1024
frame_height = 512
2013-02-19 09:10:08 +00:00
frame_ratio = poster_width / frame_height
timeline_height = 64
2013-03-22 15:07:32 +00:00
text_margin = 16
2013-02-19 09:10:08 +00:00
text_width = poster_width - 2 * text_margin
poster_image = Image.new('RGB', (poster_width, poster_height))
draw = ImageDraw.Draw(poster_image)
font_file = os.path.join(static_root, 'SourceSansProSemibold.ttf')
font_size = {'cinema': 117, 'director': 32, 'title': 48, 'year': 426}
2013-03-22 09:36:03 +00:00
font_lightness = {'cinema': 0.7, 'director': 0.8, 'title': 0.8, 'year': 0.6}
2013-02-19 09:10:08 +00:00
poster_lightness = {'image': 0.2, 'text': 0.4}
if year:
hue = int(year) % 100 / 100 * 360
saturation = 1
else:
hue = 0
saturation = 0
# background
draw.rectangle(
[(0, frame_height), (poster_width, poster_height - timeline_height)],
fill=getRGB((hue, saturation, poster_lightness['text']))
)
# year
if year:
drawText(
poster_image,
2013-03-22 07:28:47 +00:00
(-93, poster_height - timeline_height - font_size['year'] + 6),
2013-02-19 09:10:08 +00:00
year,
font_file,
font_size['year'],
getRGB((hue, saturation, font_lightness['year']))
)
# cinema
for y in [-1, 1]:
for x in [-1, 1]:
drawText(
poster_image,
(-10 + x, poster_height - timeline_height - font_size['cinema'] + 1 + y),
'Indiancine.ma',
font_file,
font_size['cinema'],
getRGB((hue, saturation, poster_lightness['text']))
)
2013-02-19 09:10:08 +00:00
drawText(
poster_image,
(-10, poster_height - timeline_height - font_size['cinema'] + 1),
2013-02-19 09:10:08 +00:00
'Indiancine.ma',
font_file,
font_size['cinema'],
getRGB((hue, saturation, font_lightness['cinema']))
)
# director and title
offset_top = frame_height + text_margin
if not director:
title_max_lines = 8
2013-02-19 09:10:08 +00:00
else:
title_max_lines = min(len(wrapText(
title,
text_width,
0,
font_file,
font_size['title']
)), 7)
director_max_lines = 11 - int((title_max_lines * 3 - 1) / 2)
2013-02-19 09:10:08 +00:00
# director
if director:
lines = wrapText(
director,
text_width,
director_max_lines,
font_file,
font_size['director']
)
for line in lines:
2013-03-22 15:32:14 +00:00
for y in [-1, 1]:
for x in [-1, 1]:
drawText(
poster_image,
(text_margin + x, offset_top + y),
line,
font_file,
font_size['director'],
getRGB((hue, saturation, poster_lightness['text']))
)
2013-02-19 09:10:08 +00:00
size = drawText(
poster_image,
(text_margin, offset_top),
line,
font_file,
font_size['director'],
getRGB((hue, saturation, font_lightness['director']))
)
2013-03-22 15:07:32 +00:00
offset_top += font_size['director'] + 2
offset_top += size[1] - font_size['director'] - 2
2013-02-19 09:10:08 +00:00
# title
lines = wrapText(
title,
text_width,
title_max_lines,
font_file,
font_size['title']
)
for line in lines:
for y in [-1, 1]:
for x in [-1, 1]:
drawText(
poster_image,
2013-03-22 15:07:32 +00:00
(text_margin + x, offset_top + y),
line,
font_file,
font_size['title'],
getRGB((hue, saturation, poster_lightness['text']))
)
2013-02-19 09:10:08 +00:00
drawText(
poster_image,
2013-03-22 15:07:32 +00:00
(text_margin, offset_top),
2013-02-19 09:10:08 +00:00
line,
font_file,
font_size['title'],
getRGB((hue, saturation, font_lightness['title']))
)
2013-03-22 15:07:32 +00:00
offset_top += font_size['title'] + 3
2013-02-19 09:10:08 +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:
frame_image = frame_image.resize(
(int(frame_height * frame_image_ratio), frame_height),
2023-08-24 21:39:54 +00:00
Image.LANCZOS
2013-02-19 09:10:08 +00:00
)
left = int((frame_image.size[0] - poster_width) / 2)
frame_image = frame_image.crop(
(left, 0, left + poster_width, frame_height)
)
else:
frame_image = frame_image.resize(
(poster_width, int(poster_width / frame_image_ratio)),
2023-08-24 21:39:54 +00:00
Image.LANCZOS
2013-02-19 09:10:08 +00:00
)
top = int((frame_image.size[1] - frame_height) / 2)
frame_image = frame_image.crop(
(0, top, poster_width, top + frame_height)
)
poster_image.paste(frame_image, (0, 0))
else:
draw.rectangle(
[(0, 0), (poster_width, frame_height)],
fill=getRGB((hue, saturation, poster_lightness['image']))
)
# timeline
if timeline:
timeline_image = Image.open(timeline)
timeline_image = timeline_image.resize(
(poster_width, timeline_height),
2023-08-24 21:39:54 +00:00
Image.LANCZOS
2013-02-19 09:10:08 +00:00
)
poster_image.paste(timeline_image, (0, poster_height - timeline_height))
else:
draw.rectangle(
[(0, poster_height - timeline_height), (poster_width, poster_height)],
fill=getRGB((hue, saturation, poster_lightness['image']))
)
2013-02-19 11:57:43 +00:00
poster_image.save(poster, quality=100)
2013-02-19 09:10:08 +00:00
def main():
parser = OptionParser()
parser.add_option('-d', '--data', dest='data', help='json file with metadata', default=None)
parser.add_option('-p', '--poster', dest='poster', help='Poster (image file to be written)')
2013-02-19 09:10:08 +00:00
(options, args) = parser.parse_args()
if None in (options.data, options.poster):
2013-02-19 09:10:08 +00:00
parser.print_help()
sys.exit()
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)
2013-02-19 09:10:08 +00:00
2017-11-02 21:11:09 +00:00
2013-02-19 09:10:08 +00:00
if __name__ == "__main__":
main()