--- /dev/null
+# 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),
+ ),
+ ]
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)
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(',')])
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',
(
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 = 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])
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