mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-27 12:01:14 +00:00
Linting fixes for preview image code
This commit is contained in:
parent
a20c4d583c
commit
d8d6f57373
3 changed files with 146 additions and 133 deletions
|
@ -1,12 +1,12 @@
|
|||
""" Generate preview images """
|
||||
import sys
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from bookwyrm import activitystreams, models, settings, preview_images
|
||||
from bookwyrm import models, preview_images
|
||||
|
||||
|
||||
# pylint: disable=line-too-long
|
||||
class Command(BaseCommand):
|
||||
"""Creates previews for existing objects"""
|
||||
help = "Generate preview images"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
import colorsys
|
||||
""" Generate social media preview images for twitter/mastodon/etc """
|
||||
import math
|
||||
import os
|
||||
import textwrap
|
||||
|
||||
from colorthief import ColorThief
|
||||
from io import BytesIO
|
||||
from PIL import Image, ImageDraw, ImageFont, ImageOps, ImageColor
|
||||
from uuid import uuid4
|
||||
|
||||
import colorsys
|
||||
from colorthief import ColorThief
|
||||
from PIL import Image, ImageDraw, ImageFont, ImageOps, ImageColor
|
||||
|
||||
from django.core.files.base import ContentFile
|
||||
from django.core.files.uploadedfile import InMemoryUploadedFile
|
||||
from django.db.models import Avg
|
||||
|
@ -31,6 +32,7 @@ font_dir = os.path.join(settings.STATIC_ROOT, "fonts/public_sans")
|
|||
|
||||
|
||||
def get_font(font_name, size=28):
|
||||
"""Loads custom font"""
|
||||
if font_name == "light":
|
||||
font_path = os.path.join(font_dir, "PublicSans-Light.ttf")
|
||||
if font_name == "regular":
|
||||
|
@ -47,6 +49,7 @@ def get_font(font_name, size=28):
|
|||
|
||||
|
||||
def generate_texts_layer(texts, content_width):
|
||||
"""Adds text for images"""
|
||||
font_text_zero = get_font("bold", size=20)
|
||||
font_text_one = get_font("bold", size=48)
|
||||
font_text_two = get_font("bold", size=40)
|
||||
|
@ -66,7 +69,7 @@ def generate_texts_layer(texts, content_width):
|
|||
|
||||
try:
|
||||
text_y = text_y + font_text_zero.getsize_multiline(text_zero)[1] + 16
|
||||
except:
|
||||
except (AttributeError, IndexError):
|
||||
text_y = text_y + 26
|
||||
|
||||
if "text_one" in texts and texts["text_one"]:
|
||||
|
@ -78,7 +81,7 @@ def generate_texts_layer(texts, content_width):
|
|||
|
||||
try:
|
||||
text_y = text_y + font_text_one.getsize_multiline(text_one)[1] + 16
|
||||
except:
|
||||
except (AttributeError, IndexError):
|
||||
text_y = text_y + 26
|
||||
|
||||
if "text_two" in texts and texts["text_two"]:
|
||||
|
@ -90,7 +93,7 @@ def generate_texts_layer(texts, content_width):
|
|||
|
||||
try:
|
||||
text_y = text_y + font_text_one.getsize_multiline(text_two)[1] + 16
|
||||
except:
|
||||
except (AttributeError, IndexError):
|
||||
text_y = text_y + 26
|
||||
|
||||
if "text_three" in texts and texts["text_three"]:
|
||||
|
@ -105,6 +108,7 @@ def generate_texts_layer(texts, content_width):
|
|||
|
||||
|
||||
def generate_instance_layer(content_width):
|
||||
"""Places components for instance preview"""
|
||||
font_instance = get_font("light", size=28)
|
||||
|
||||
site = models.SiteSettings.objects.get()
|
||||
|
@ -145,7 +149,7 @@ def generate_instance_layer(content_width):
|
|||
|
||||
|
||||
def generate_rating_layer(rating, content_width):
|
||||
try:
|
||||
"""Places components for rating preview"""
|
||||
icon_star_full = Image.open(
|
||||
os.path.join(settings.STATIC_ROOT, "images/icons/star-full.png")
|
||||
)
|
||||
|
@ -171,7 +175,7 @@ def generate_rating_layer(rating, content_width):
|
|||
|
||||
position_x = 0
|
||||
|
||||
for r in range(math.floor(rating)):
|
||||
for _ in range(math.floor(rating)):
|
||||
rating_layer_mask.alpha_composite(icon_star_full, (position_x, 0))
|
||||
position_x = position_x + icon_size + icon_margin
|
||||
|
||||
|
@ -179,7 +183,7 @@ def generate_rating_layer(rating, content_width):
|
|||
rating_layer_mask.alpha_composite(icon_star_half, (position_x, 0))
|
||||
position_x = position_x + icon_size + icon_margin
|
||||
|
||||
for r in range(5 - math.ceil(rating)):
|
||||
for _ in range(5 - math.ceil(rating)):
|
||||
rating_layer_mask.alpha_composite(icon_star_empty, (position_x, 0))
|
||||
position_x = position_x + icon_size + icon_margin
|
||||
|
||||
|
@ -191,11 +195,10 @@ def generate_rating_layer(rating, content_width):
|
|||
)
|
||||
|
||||
return rating_layer_composite
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
def generate_default_inner_img():
|
||||
"""Adds cover image"""
|
||||
font_cover = get_font("light", size=28)
|
||||
|
||||
default_cover = Image.new(
|
||||
|
@ -214,16 +217,19 @@ def generate_default_inner_img():
|
|||
return default_cover
|
||||
|
||||
|
||||
# pylint: disable=too-many-locals
|
||||
def generate_preview_image(
|
||||
texts={}, picture=None, rating=None, show_instance_layer=True
|
||||
texts=None, picture=None, rating=None, show_instance_layer=True
|
||||
):
|
||||
"""Puts everything together"""
|
||||
texts = texts or {}
|
||||
# Cover
|
||||
try:
|
||||
inner_img_layer = Image.open(picture)
|
||||
inner_img_layer.thumbnail((inner_img_width, inner_img_height), Image.ANTIALIAS)
|
||||
color_thief = ColorThief(picture)
|
||||
dominant_color = color_thief.get_color(quality=1)
|
||||
except:
|
||||
except: # pylint: disable=bare-except
|
||||
inner_img_layer = generate_default_inner_img()
|
||||
dominant_color = ImageColor.getrgb(DEFAULT_COVER_COLOR)
|
||||
|
||||
|
@ -246,7 +252,7 @@ def generate_preview_image(
|
|||
image_bg_color_hls[2],
|
||||
)
|
||||
image_bg_color = tuple(
|
||||
[math.ceil(x * 255) for x in colorsys.hls_to_rgb(*image_bg_color_hls)]
|
||||
math.ceil(x * 255) for x in colorsys.hls_to_rgb(*image_bg_color_hls)
|
||||
)
|
||||
else:
|
||||
image_bg_color = BG_COLOR
|
||||
|
@ -292,8 +298,7 @@ def generate_preview_image(
|
|||
# Remove Instance Layer from centering calculations
|
||||
contents_y = contents_y - math.floor((instance_layer.height + gutter) / 2)
|
||||
|
||||
if contents_y < margin:
|
||||
contents_y = margin
|
||||
contents_y = max(contents_y, margin)
|
||||
|
||||
# Composite layers
|
||||
img.paste(
|
||||
|
@ -305,7 +310,9 @@ def generate_preview_image(
|
|||
|
||||
|
||||
def save_and_cleanup(image, instance=None):
|
||||
if isinstance(instance, (models.Book, models.User, models.SiteSettings)):
|
||||
"""Save and close the file"""
|
||||
if not isinstance(instance, (models.Book, models.User, models.SiteSettings)):
|
||||
return False
|
||||
file_name = "%s-%s.jpg" % (str(instance.id), str(uuid4()))
|
||||
image_buffer = BytesIO()
|
||||
|
||||
|
@ -329,7 +336,7 @@ def save_and_cleanup(image, instance=None):
|
|||
|
||||
save_without_broadcast = isinstance(instance, (models.Book, models.User))
|
||||
if save_without_broadcast:
|
||||
result = instance.save(broadcast=False)
|
||||
instance.save(broadcast=False)
|
||||
else:
|
||||
instance.save()
|
||||
|
||||
|
@ -340,14 +347,15 @@ def save_and_cleanup(image, instance=None):
|
|||
finally:
|
||||
image_buffer.close()
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
@app.task
|
||||
def generate_site_preview_image_task():
|
||||
"""generate preview_image for the website"""
|
||||
if settings.ENABLE_PREVIEW_IMAGES == True:
|
||||
if not settings.ENABLE_PREVIEW_IMAGES:
|
||||
return
|
||||
|
||||
site = models.SiteSettings.objects.get()
|
||||
|
||||
if site.logo:
|
||||
|
@ -368,10 +376,13 @@ def generate_site_preview_image_task():
|
|||
save_and_cleanup(image, instance=site)
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
@app.task
|
||||
def generate_edition_preview_image_task(book_id):
|
||||
"""generate preview_image for a book"""
|
||||
if settings.ENABLE_PREVIEW_IMAGES == True:
|
||||
if not settings.ENABLE_PREVIEW_IMAGES:
|
||||
return
|
||||
|
||||
book = models.Book.objects.select_subclasses().get(id=book_id)
|
||||
|
||||
rating = models.Review.objects.filter(
|
||||
|
@ -394,7 +405,9 @@ def generate_edition_preview_image_task(book_id):
|
|||
@app.task
|
||||
def generate_user_preview_image_task(user_id):
|
||||
"""generate preview_image for a book"""
|
||||
if settings.ENABLE_PREVIEW_IMAGES == True:
|
||||
if not settings.ENABLE_PREVIEW_IMAGES:
|
||||
return
|
||||
|
||||
user = models.User.objects.get(id=user_id)
|
||||
|
||||
texts = {
|
||||
|
|
|
@ -18,9 +18,9 @@ from bookwyrm.preview_images import (
|
|||
save_and_cleanup,
|
||||
)
|
||||
|
||||
import logging
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
# pylint: disable=missing-function-docstring
|
||||
class PreviewImages(TestCase):
|
||||
"""every response to a get request, html or json"""
|
||||
|
||||
|
|
Loading…
Reference in a new issue