More thumbnail options.
authorRadek Czajka <rczajka@rczajka.pl>
Mon, 6 Jul 2020 10:48:24 +0000 (12:48 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Mon, 6 Jul 2020 10:48:24 +0000 (12:48 +0200)
src/youtube/migrations/0014_auto_20200706_1219.py [new file with mode: 0644]
src/youtube/models.py
src/youtube/thumbnail.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 (file)
index 0000000..d53c7cd
--- /dev/null
@@ -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),
+        ),
+    ]
index 219a682..356e335 100644 (file)
@@ -216,6 +216,9 @@ class ThumbnailTemplate(models.Model):
     is_active = models.BooleanField()
     background = models.FileField(upload_to='youtube/thumbnail')
     definition = models.TextField()
     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)
 
     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):
             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(',')])
 
         if self.collections:
             template_collections = set([g.strip() for g in self.collections.split(',')])
index 8eb7973..e40b552 100644 (file)
@@ -22,7 +22,7 @@ def split_to_lines(text, draw, font, max_width):
         yield current
 
         
         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',
         (
     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 = 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
         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']),
             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])
             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'))
             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
 
 
 
     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']:
 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
             raise ValueError()
     return img