This commit is contained in:
Joachim 2021-05-26 14:46:34 +02:00
parent e5e549d125
commit eb56cced8d
4 changed files with 39 additions and 34 deletions

View file

@ -9,7 +9,9 @@ from bookwyrm import activitystreams, models, settings, preview_images
def generate_preview_images():
"""generate preview images"""
print(" | Hello! I will be generating preview images for your instance.")
print("🧑‍🎨 ⎨ This might take quite long if your instance has a lot of books and users.")
print(
"🧑‍🎨 ⎨ This might take quite long if your instance has a lot of books and users."
)
print(" | ✧ Thank you for your patience ✧")
# Site
@ -17,7 +19,6 @@ def generate_preview_images():
preview_images.generate_site_preview_image_task()
sys.stdout.write(" OK 🖼\n")
# Users
users = models.User.objects.filter(
local=True,

View file

@ -38,7 +38,9 @@ class SiteSettings(models.Model):
logo = models.ImageField(upload_to="logos/", null=True, blank=True)
logo_small = models.ImageField(upload_to="logos/", null=True, blank=True)
favicon = models.ImageField(upload_to="logos/", null=True, blank=True)
preview_image = models.ImageField(upload_to="previews/logos/", null=True, blank=True)
preview_image = models.ImageField(
upload_to="previews/logos/", null=True, blank=True
)
# footer
support_link = models.CharField(max_length=255, null=True, blank=True)

View file

@ -61,30 +61,36 @@ def generate_texts_layer(texts, content_width):
text_y = 0
if 'text_zero' in texts:
if "text_zero" in texts:
# Text one (Book title)
text_zero = textwrap.fill(texts['text_zero'], width=72)
text_layer_draw.multiline_text((0, text_y), text_zero, font=font_text_zero, fill=TEXT_COLOR)
text_zero = textwrap.fill(texts["text_zero"], width=72)
text_layer_draw.multiline_text(
(0, text_y), text_zero, font=font_text_zero, fill=TEXT_COLOR
)
text_y = text_y + font_text_zero.getsize_multiline(text_zero)[1] + 16
if 'text_one' in texts:
if "text_one" in texts:
# Text one (Book title)
text_one = textwrap.fill(texts['text_one'], width=28)
text_layer_draw.multiline_text((0, text_y), text_one, font=font_text_one, fill=TEXT_COLOR)
text_one = textwrap.fill(texts["text_one"], width=28)
text_layer_draw.multiline_text(
(0, text_y), text_one, font=font_text_one, fill=TEXT_COLOR
)
text_y = text_y + font_text_one.getsize_multiline(text_one)[1] + 16
if 'text_two' in texts:
if "text_two" in texts:
# Text one (Book subtitle)
text_two = textwrap.fill(texts['text_two'], width=36)
text_layer_draw.multiline_text((0, text_y), text_two, font=font_text_two, fill=TEXT_COLOR)
text_two = textwrap.fill(texts["text_two"], width=36)
text_layer_draw.multiline_text(
(0, text_y), text_two, font=font_text_two, fill=TEXT_COLOR
)
text_y = text_y + font_text_one.getsize_multiline(text_two)[1] + 16
if 'text_three' in texts:
if "text_three" in texts:
# Text three (Book authors)
text_three = textwrap.fill(texts['text_three'], width=36)
text_three = textwrap.fill(texts["text_three"], width=36)
text_layer_draw.multiline_text(
(0, text_y), text_three, font=font_text_three, fill=TEXT_COLOR
)
@ -183,7 +189,9 @@ def generate_default_inner_img():
return default_cover
def generate_preview_image(texts={}, picture=None, rating=None, show_instance_layer=True):
def generate_preview_image(
texts={}, picture=None, rating=None, show_instance_layer=True
):
# Cover
try:
inner_img_layer = Image.open(picture)
@ -194,7 +202,6 @@ def generate_preview_image(texts={}, picture=None, rating=None, show_instance_la
inner_img_layer = generate_default_inner_img()
dominant_color = ImageColor.getrgb(DEFAULT_COVER_COLOR)
# Color
if BG_COLOR in ["use_dominant_color_light", "use_dominant_color_dark"]:
image_bg_color = "rgb(%s, %s, %s)" % dominant_color
@ -262,7 +269,7 @@ def generate_preview_image(texts={}, picture=None, rating=None, show_instance_la
cover_y = math.floor((IMG_HEIGHT - inner_img_layer.height) / 2)
# Composite layers
img.paste(inner_img_layer, (margin, cover_y), inner_img_layer.convert('RGBA'))
img.paste(inner_img_layer, (margin, cover_y), inner_img_layer.convert("RGBA"))
img.alpha_composite(contents_layer, (content_x, contents_y))
return img
@ -309,14 +316,12 @@ def generate_site_preview_image_task():
logo = path.joinpath("static/images/logo.png")
texts = {
'text_zero': DOMAIN,
'text_one': site.name,
'text_three': site.instance_tagline,
"text_zero": DOMAIN,
"text_one": site.name,
"text_three": site.instance_tagline,
}
image = generate_preview_image(texts=texts,
picture=logo,
show_instance_layer=False)
image = generate_preview_image(texts=texts, picture=logo, show_instance_layer=False)
save_and_cleanup(image, instance=site)
@ -333,14 +338,12 @@ def generate_edition_preview_image_task(book_id):
).aggregate(Avg("rating"))["rating__avg"]
texts = {
'text_one': book.title,
'text_two': book.subtitle,
'text_three': book.author_text
"text_one": book.title,
"text_two": book.subtitle,
"text_three": book.author_text,
}
image = generate_preview_image(texts=texts,
picture=book.cover,
rating=rating)
image = generate_preview_image(texts=texts, picture=book.cover, rating=rating)
save_and_cleanup(image, instance=book)
@ -351,8 +354,8 @@ def generate_user_preview_image_task(user_id):
user = models.User.objects.get(id=user_id)
texts = {
'text_one': user.display_name,
'text_three': "@{}@{}".format(user.localname, DOMAIN)
"text_one": user.display_name,
"text_three": "@{}@{}".format(user.localname, DOMAIN),
}
if user.avatar:
@ -360,7 +363,6 @@ def generate_user_preview_image_task(user_id):
else:
avatar = path.joinpath("static/images/default_avi.jpg")
image = generate_preview_image(texts=texts,
picture=avatar)
image = generate_preview_image(texts=texts, picture=avatar)
save_and_cleanup(image, instance=user)

View file

@ -44,7 +44,7 @@ DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
PREVIEW_BG_COLOR = "use_dominant_color_light"
PREVIEW_IMG_WIDTH = 1200
PREVIEW_IMG_HEIGHT = 630
PREVIEW_TEXT_COLOR = "#363636" # Change to "#FFF" if you use "use_dominant_color_dark"
PREVIEW_TEXT_COLOR = "#363636" # Change to "#FFF" if you use "use_dominant_color_dark"
PREVIEW_DEFAULT_COVER_COLOR = "#002549"
# Quick-start development settings - unsuitable for production