resize gifs, links to content/users

master
lza_menace 2 years ago
parent 02d218bf7a
commit fbf51acab6

@ -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

@ -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()

@ -6,9 +6,9 @@ from nerochan.models import Artwork, User
bp = Blueprint('artwork', 'artwork', url_prefix='/artwork')
@bp.route('/<int:artwork_id>')
def show(artwork_id):
artwork = Artwork.get_or_none(artwork_id)
@bp.route('/<int:id>')
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'))

@ -1,25 +1,17 @@
<!DOCTYPE HTML>
<html>
{% extends 'includes/base.html' %}
{% include 'includes/head.html' %}
{% block content %}
<body class="is-preload landing">
<div id="page-wrapper">
{% set img = url_for('main.uploaded_file', filename=artwork.image) %}
<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 %}
<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>
{% endblock %}

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

@ -2,7 +2,15 @@
{% 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 %}

Loading…
Cancel
Save