Skip to content
Snippets Groups Projects
Commit 3c47480d authored by Julian's avatar Julian
Browse files

Refactor thumbnail code

parent 8750f266
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,7 @@ from flask import Flask, render_template, request, redirect, url_for, Response,
from requests_oauthlib import OAuth2Session
from .models import db, Item, Location, Photo
from .utils import render_pdf, print_pdf, qrcode_svg, render_markdown, send_thumbnail
from .utils import render_pdf, print_pdf, qrcode_svg, render_markdown
app = Flask(__name__, instance_relative_config=True)
app.config.from_pyfile('config.cfg')
......@@ -185,13 +185,11 @@ def photo_show(photo_id):
@app.route('/photos/<int:photo_id>/small')
def photo_small(photo_id):
photo = Photo.query.get_or_404(photo_id)
return send_thumbnail(photo.image, 130, 100)
return Photo.query.get_or_404(photo_id).send_thumbnail(130, 100)
@app.route('/photos/<int:photo_id>/medium')
def photo_medium(photo_id):
photo = Photo.query.get_or_404(photo_id)
return send_thumbnail(photo.image, 390, 300)
return Photo.query.get_or_404(photo_id).send_thumbnail(390, 300)
@app.route('/C/<code>')
@app.route('/c/<code>')
......
import secrets
import os
import io
from flask import current_app, send_file
from flask import current_app, send_file, Markup
from flask_sqlalchemy import SQLAlchemy
from PIL import Image
......@@ -42,10 +43,15 @@ class Photo(db.Model):
path = os.path.join(current_app.config['UPLOAD_FOLDER'], self.path)
return send_file(path, mimetype=self.mimetype)
@property
def image(self):
def send_thumbnail(self, max_width, max_height):
path = os.path.join(current_app.config['UPLOAD_FOLDER'], self.path)
return Image.open(path, formats=['JPEG'])
buf = io.BytesIO()
with Image.open(path, formats=['JPEG']) as img:
scale = min(max_width/img.width, max_height/img.height)
img.thumbnail((int(img.width*scale), int(img.height*scale)))
img.save(buf, format='JPEG', quality=70)
buf.seek(0)
return send_file(buf, mimetype='image/jpeg')
def token_typable():
alphabet = '23456789ABCDEFGHJKLMNPQRSTUVWX' # not '01OIYZ'
......
......@@ -2,8 +2,7 @@ from .pdf import render_pdf
from .ipp import print_pdf
from .codes import qrcode_svg
import io
from flask import Markup, send_file
from flask import Markup
import markdown
import bleach
......@@ -17,11 +16,3 @@ def render_markdown(text, short=False):
result = result.split('<p>', 1)[-1]
result = result.split('</p>', 1)[0]
return Markup(result)
def send_thumbnail(img, max_width, max_height):
scale = min(max_width/img.width, max_height/img.height)
img.thumbnail((int(img.width*scale), int(img.height*scale)))
buf = io.BytesIO()
img.save(buf, format='JPEG', quality=70)
buf.seek(0)
return send_file(buf, mimetype='image/jpeg')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment