From fbf51acab63c81a884aea48c0b55affa53ad0c56 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Sun, 27 Nov 2022 20:13:12 -0800 Subject: [PATCH] resize gifs, links to content/users --- nerochan/cli.py | 5 +++- nerochan/models.py | 20 +++++++++++++--- nerochan/routes/artwork.py | 6 ++--- nerochan/templates/artwork/show.html | 34 +++++++++++----------------- nerochan/templates/index.html | 14 +++++++----- nerochan/templates/user/show.html | 12 ++++++++-- 6 files changed, 55 insertions(+), 36 deletions(-) diff --git a/nerochan/cli.py b/nerochan/cli.py index d7c0cac..0309572 100644 --- a/nerochan/cli.py +++ b/nerochan/cli.py @@ -63,6 +63,9 @@ def cli(app): description='' ) artwork.save() - artwork.generate_thumbnail() click.echo(f'[+] Created artwork {artwork.id} for {bn}') + if not path.exists(f'./data/uploads/thumbnail-{bn}'): + artwork = Artwork.select().where(Artwork.image == bn).first() + artwork.generate_thumbnail() + click.echo(f'[+] Generated thumbnail for {bn}') return app diff --git a/nerochan/models.py b/nerochan/models.py index 93694dc..3463c26 100644 --- a/nerochan/models.py +++ b/nerochan/models.py @@ -2,7 +2,7 @@ from os import path from datetime import datetime from secrets import token_urlsafe -from PIL import Image +from PIL import Image, ImageSequence import peewee as pw @@ -91,15 +91,29 @@ class Artwork(pw.Model): description = pw.TextField(null=True) def generate_thumbnail(self): + is_gif = self.image.endswith('.gif') _t = f'thumbnail-{self.image}' i = f'{config.DATA_PATH}/uploads/{self.image}' t = f'{config.DATA_PATH}/uploads/{_t}' if path.exists(t): return True try: + size = (150,150) image = Image.open(i) - image.thumbnail((150,150), Image.ANTIALIAS) - image.save(t, format=image.format, quality=75) + if is_gif: + frames = ImageSequence.Iterator(image) + def thumbnails(frames): + for frame in frames: + thumbnail = frame.copy() + thumbnail.thumbnail(size, Image.ANTIALIAS) + yield thumbnail + _frames = thumbnails(frames) + _image = next(_frames) + _image.info = image.info + _image.save(t, format=image.format, save_all=True, append_images=list(_frames), disposal=2) + else: + image.thumbnail(size, Image.ANTIALIAS) + image.save(t, format=image.format, quality=75) image.close() self.thumbnail = _t self.save() diff --git a/nerochan/routes/artwork.py b/nerochan/routes/artwork.py index 57dbca8..4a76124 100644 --- a/nerochan/routes/artwork.py +++ b/nerochan/routes/artwork.py @@ -6,9 +6,9 @@ from nerochan.models import Artwork, User bp = Blueprint('artwork', 'artwork', url_prefix='/artwork') -@bp.route('/') -def show(artwork_id): - artwork = Artwork.get_or_none(artwork_id) +@bp.route('/') +def show(id): + artwork = Artwork.get_or_none(id) if not artwork: flash('That artwork does not exist.', 'warning') return redirect(url_for('main.index')) diff --git a/nerochan/templates/artwork/show.html b/nerochan/templates/artwork/show.html index 80d20b4..6e4dd49 100644 --- a/nerochan/templates/artwork/show.html +++ b/nerochan/templates/artwork/show.html @@ -1,25 +1,17 @@ - - +{% extends 'includes/base.html' %} - {% include 'includes/head.html' %} +{% block content %} - -
+{% set img = url_for('main.uploaded_file', filename=artwork.image) %} +
+
+

{{ artwork.title }}

+

{{ artwork.creator.handle }}

+ + + +
+
- {% include 'includes/header.html' %} - {% if post %} -

{{ post.title }}

-

Posted: {{ post.post_date }}

-

Edited: {{ post.last_edit_date }}

-

{{ post.content }}

- {% endif %} - - {% include 'includes/footer.html' %} - -
- - {% include 'includes/scripts.html' %} - - - +{% endblock %} diff --git a/nerochan/templates/index.html b/nerochan/templates/index.html index 82a8779..c8e4d49 100644 --- a/nerochan/templates/index.html +++ b/nerochan/templates/index.html @@ -5,19 +5,21 @@

Latest Artworks

- {% for artwork in feed['artwork'] %} -

{{ artwork.title }}

- + {% for _artwork in feed['artwork'] | batch(4) %} + {% for artwork in _artwork %} + + + + {% endfor %} {% endfor %}

Latest Artists

{% for user in feed['users'] %} -

{{ user.handle }}

+

{{ user.handle }}

{% endfor %}
-
- + {% if current_user.is_authenticated %}

logged in: {{ current_user.handle }}

diff --git a/nerochan/templates/user/show.html b/nerochan/templates/user/show.html index 5f64792..617068a 100644 --- a/nerochan/templates/user/show.html +++ b/nerochan/templates/user/show.html @@ -2,7 +2,15 @@ {% block content %} -

{{ user.handle }}

-

User details

+ + +
+
+

{{ user.handle }}

+

User details

+
+
+ + {% endblock %}