resize gifs, links to content/users

master
lza_menace 2 years ago
parent 02d218bf7a
commit fbf51acab6

@ -63,6 +63,9 @@ def cli(app):
description='' description=''
) )
artwork.save() artwork.save()
artwork.generate_thumbnail()
click.echo(f'[+] Created artwork {artwork.id} for {bn}') 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 return app

@ -2,7 +2,7 @@ from os import path
from datetime import datetime from datetime import datetime
from secrets import token_urlsafe from secrets import token_urlsafe
from PIL import Image from PIL import Image, ImageSequence
import peewee as pw import peewee as pw
@ -91,15 +91,29 @@ class Artwork(pw.Model):
description = pw.TextField(null=True) description = pw.TextField(null=True)
def generate_thumbnail(self): def generate_thumbnail(self):
is_gif = self.image.endswith('.gif')
_t = f'thumbnail-{self.image}' _t = f'thumbnail-{self.image}'
i = f'{config.DATA_PATH}/uploads/{self.image}' i = f'{config.DATA_PATH}/uploads/{self.image}'
t = f'{config.DATA_PATH}/uploads/{_t}' t = f'{config.DATA_PATH}/uploads/{_t}'
if path.exists(t): if path.exists(t):
return True return True
try: try:
size = (150,150)
image = Image.open(i) image = Image.open(i)
image.thumbnail((150,150), Image.ANTIALIAS) if is_gif:
image.save(t, format=image.format, quality=75) 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() image.close()
self.thumbnail = _t self.thumbnail = _t
self.save() self.save()

@ -6,9 +6,9 @@ from nerochan.models import Artwork, User
bp = Blueprint('artwork', 'artwork', url_prefix='/artwork') bp = Blueprint('artwork', 'artwork', url_prefix='/artwork')
@bp.route('/<int:artwork_id>') @bp.route('/<int:id>')
def show(artwork_id): def show(id):
artwork = Artwork.get_or_none(artwork_id) artwork = Artwork.get_or_none(id)
if not artwork: if not artwork:
flash('That artwork does not exist.', 'warning') flash('That artwork does not exist.', 'warning')
return redirect(url_for('main.index')) return redirect(url_for('main.index'))

@ -1,25 +1,17 @@
<!DOCTYPE HTML> {% extends 'includes/base.html' %}
<html>
{% include 'includes/head.html' %} {% block content %}
<body class="is-preload landing"> {% set img = url_for('main.uploaded_file', filename=artwork.image) %}
<div id="page-wrapper"> <div class="container">
<div class="row">
<h2>{{ artwork.title }}</h2>
<p>{{ artwork.creator.handle }}</p>
<a href="{{ img }}">
<img src="{{ img }}" width="450px">
</a>
</div>
</div>
{% include 'includes/header.html' %}
{% if post %} {% endblock %}
<h1>{{ post.title }}</h1>
<p>Posted: {{ post.post_date }}</p>
<p>Edited: {{ post.last_edit_date }}</p>
<p>{{ post.content }}</p>
{% endif %}
{% include 'includes/footer.html' %}
</div>
{% include 'includes/scripts.html' %}
</body>
</html>

@ -5,19 +5,21 @@
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<h2>Latest Artworks</h2> <h2>Latest Artworks</h2>
{% for artwork in feed['artwork'] %} {% for _artwork in feed['artwork'] | batch(4) %}
<p>{{ artwork.title }}</p> {% for artwork in _artwork %}
<img src="{{ url_for('main.uploaded_file', filename=artwork.thumbnail) }}" width="120px"> <a href="{{ url_for('artwork.show', id=artwork.id) }}">
<img src="{{ url_for('main.uploaded_file', filename=artwork.thumbnail) }}" width="150px">
</a>
{% endfor %}
{% endfor %} {% endfor %}
</div> </div>
<div class="row"> <div class="row">
<h2>Latest Artists</h2> <h2>Latest Artists</h2>
{% for user in feed['users'] %} {% for user in feed['users'] %}
<p>{{ user.handle }}</p> <p><a href="{{ url_for('user.show', handle=user.handle) }}">{{ user.handle }}</a></p>
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
</div>
{% if current_user.is_authenticated %} {% if current_user.is_authenticated %}
<p>logged in: {{ current_user.handle }}</p> <p>logged in: {{ current_user.handle }}</p>

@ -2,7 +2,15 @@
{% block content %} {% block content %}
<h1>{{ user.handle }}</h1>
<p>User details</p>
<div class="container">
<div class="row">
<h1>{{ user.handle }}</h1>
<p>User details</p>
</div>
</div>
{% endblock %} {% endblock %}

Loading…
Cancel
Save