From 1b434b4f4eb775a20054b0efeca3fea63a161879 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 6 Jul 2020 12:48:24 +0200 Subject: [PATCH] More thumbnail options. --- .../migrations/0014_auto_20200706_1219.py | 28 +++++++++++++++++++ src/youtube/models.py | 14 ++++++---- src/youtube/thumbnail.py | 19 +++++++++---- 3 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 src/youtube/migrations/0014_auto_20200706_1219.py diff --git a/src/youtube/migrations/0014_auto_20200706_1219.py b/src/youtube/migrations/0014_auto_20200706_1219.py new file mode 100644 index 0000000..d53c7cd --- /dev/null +++ b/src/youtube/migrations/0014_auto_20200706_1219.py @@ -0,0 +1,28 @@ +# Generated by Django 3.0.6 on 2020-07-06 12:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('youtube', '0013_auto_20200706_1123'), + ] + + operations = [ + migrations.AddField( + model_name='thumbnailtemplate', + name='authors', + field=models.CharField(blank=True, max_length=255), + ), + migrations.AddField( + model_name='thumbnailtemplate', + name='epochs', + field=models.CharField(blank=True, max_length=255), + ), + migrations.AddField( + model_name='thumbnailtemplate', + name='kinds', + field=models.CharField(blank=True, max_length=255), + ), + ] diff --git a/src/youtube/models.py b/src/youtube/models.py index 219a682..356e335 100644 --- a/src/youtube/models.py +++ b/src/youtube/models.py @@ -216,6 +216,9 @@ class ThumbnailTemplate(models.Model): is_active = models.BooleanField() background = models.FileField(upload_to='youtube/thumbnail') definition = models.TextField() + authors = models.CharField(max_length=255, blank=True) + epochs = models.CharField(max_length=255, blank=True) + kinds = models.CharField(max_length=255, blank=True) genres = models.CharField(max_length=255, blank=True) collections = models.CharField(max_length=255, blank=True) @@ -243,11 +246,12 @@ class ThumbnailTemplate(models.Model): return buf def is_for_audiobook(self, audiobook): - if self.genres: - book_genres = set([g['slug'] for g in audiobook.book['genres']]) - template_genres = set([g.strip() for g in self.genres.split(',')]) - if not book_genres.intersection(template_genres): - return False + for category in 'authors', 'epochs', 'kinds', 'genres': + if getattr(self, category): + book_slugs = set([g['slug'] for g in audiobook.book[category]]) + template_slugs = set([g.strip() for g in getattr(self, category).split(',')]) + if not book_slugs.intersection(template_slugs): + return False if self.collections: template_collections = set([g.strip() for g in self.collections.split(',')]) diff --git a/src/youtube/thumbnail.py b/src/youtube/thumbnail.py index 8eb7973..e40b552 100644 --- a/src/youtube/thumbnail.py +++ b/src/youtube/thumbnail.py @@ -22,7 +22,7 @@ def split_to_lines(text, draw, font, max_width): yield current -def draw_box(img, d, context, get_font_path): +def draw_box(img, d, context, get_font_path, scale): newimg = Image.new( 'RGBA', ( @@ -35,7 +35,7 @@ def draw_box(img, d, context, get_font_path): cursor = 0 for item in d['items']: if item.get('vskip'): - cursor += item['vskip'] + cursor += int(round(item['vskip'] * scale)) text = item['text'].format(**context) if not text: continue @@ -43,7 +43,7 @@ def draw_box(img, d, context, get_font_path): text = text.upper() font = ImageFont.truetype( get_font_path(item['font-family']), - item['font-size'], + int(round(item['font-size'] * scale)), layout_engine=ImageFont.LAYOUT_BASIC ) max_width = item.get('max-width', newimg.size[0]) @@ -53,16 +53,25 @@ def draw_box(img, d, context, get_font_path): if cursor + realheight > newimg.size[1]: return False draw.text((0, cursor), line, font=font, fill=item.get('color')) - cursor += item['line-height'] + cursor += int(round(item['line-height'] * scale)) img.paste(newimg, (d.get('x', 0), d.get('y', 0)), newimg) return True +def draw_box_with_scaling(img, d, context, get_font_path): + scale = 1.0 + while scale > 0: + if draw_box(img, d, context, get_font_path, scale): + return True + scale -= 0.05 + + + def create_thumbnail(background_path, defn, context, get_font_path): img = Image.open(background_path) d = yaml.load(defn) for boxdef in d['boxes']: - if not draw_box(img, boxdef, context, get_font_path): + if not draw_box_with_scaling(img, boxdef, context, get_font_path): raise ValueError() return img -- 2.20.1