From 959479457d93ac8dba5e0785b20013b9bb87b63d Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 26 Jun 2013 14:14:10 +0200 Subject: [PATCH] get frame via manage.py so it also works for private items --- pandora/item/management/commands/get_frame.py | 16 ++++++++ scripts/poster.0xdb.py | 38 ++++++++++++------- scripts/poster.arsenalberlin.py | 38 ++++++++++++------- 3 files changed, 66 insertions(+), 26 deletions(-) create mode 100644 pandora/item/management/commands/get_frame.py diff --git a/pandora/item/management/commands/get_frame.py b/pandora/item/management/commands/get_frame.py new file mode 100644 index 000000000..37f4e1f58 --- /dev/null +++ b/pandora/item/management/commands/get_frame.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 +from django.core.management.base import BaseCommand + +import app.monkey_patch +from ... import models + +class Command(BaseCommand): + help = 'extract frame and print path' + args = ' ' + + def handle(self, id, height, position, **options): + position = float(position) + i = models.Item.objects.get(itemId=id) + path = i.frame(position, height) + print path diff --git a/scripts/poster.0xdb.py b/scripts/poster.0xdb.py index 121c737d6..eb37aea24 100755 --- a/scripts/poster.0xdb.py +++ b/scripts/poster.0xdb.py @@ -16,11 +16,23 @@ import json from optparse import OptionParser import ox from ox.image import drawText, getRGB, getTextSize, wrapText -from StringIO import StringIO +import subprocess import sys static_root = os.path.join(os.path.dirname(__file__), 'data') +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 + def render_poster(data, poster): title = ox.decode_html(data.get('title', '')).upper() @@ -83,18 +95,18 @@ def render_poster(data, poster): if duration: for i in range(small_frames): position = duration * (i + 1) / (small_frames + 1) - small_frame_url = 'https://0xdb.org/%s/96p%f.jpg' % (id, round(position * 25) / 25) - small_frame_image = Image.open(StringIO(ox.net.read_url(small_frame_url))) - 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])) + 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])) else: draw.rectangle(((0, frame_size[1]), (poster_size[0], frame_size[1] + small_frame_size[1])), fill=image_color) diff --git a/scripts/poster.arsenalberlin.py b/scripts/poster.arsenalberlin.py index 75b89bc06..22a19d2d3 100755 --- a/scripts/poster.arsenalberlin.py +++ b/scripts/poster.arsenalberlin.py @@ -17,11 +17,23 @@ import json from optparse import OptionParser import ox from ox.image import drawText, getRGB, getTextSize, wrapText -from StringIO import StringIO +import subprocess import sys static_root = os.path.join(os.path.dirname(__file__), 'data') +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 + def render_poster(data, poster): director = ox.decode_html(u', '.join(data.get('director', []))) @@ -74,18 +86,18 @@ def render_poster(data, poster): if duration: for i in range(small_frames): position = duration * (i + 1) / (small_frames + 1) - small_frame_url = 'http://arsenalberl.in/%s/96p%f.jpg' % (id, round(position * 25) / 25) - small_frame_image = Image.open(StringIO(ox.net.read_url(small_frame_url))) - 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])) + 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])) else: draw.rectangle(((0, frame_size[1]), (poster_size[0], frame_size[1] + small_frame_size[1])), fill=image_color)