forked from emilevs/label_printer
Working flask server with simple image upload
This commit is contained in:
parent
d6c4bab52e
commit
a6163cff0a
4 changed files with 103 additions and 0 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1 +1,4 @@
|
|||
*.so
|
||||
*.swp
|
||||
*/__pycache__
|
||||
flask/static
|
||||
|
|
46
flask/app.py
Normal file
46
flask/app.py
Normal file
|
@ -0,0 +1,46 @@
|
|||
from flask import Flask, render_template, request, redirect, url_for
|
||||
from PIL import Image
|
||||
import os
|
||||
|
||||
valid_height=800
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['UPLOAD_FOLDER'] = 'static/uploads'
|
||||
|
||||
# Ensure the upload directory exists
|
||||
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
||||
os.makedirs(app.config['UPLOAD_FOLDER'])
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def upload_image():
|
||||
if request.method == 'POST':
|
||||
if 'image' not in request.files:
|
||||
return redirect(request.url)
|
||||
file = request.files['image']
|
||||
|
||||
if file.filename == '':
|
||||
return redirect(request.url)
|
||||
if file:
|
||||
extension = os.path.splitext(file.filename)[1]
|
||||
filepath = os.path.join(app.config['UPLOAD_FOLDER'], "upload" + extension)
|
||||
|
||||
file.save(filepath)
|
||||
|
||||
# Process image #
|
||||
height = Image.open(filepath).height
|
||||
|
||||
valid = False
|
||||
if height == valid_height:
|
||||
valid = True
|
||||
#################
|
||||
|
||||
return render_template('image.html', filename="upload"+extension, height=height, valid=valid)
|
||||
return render_template('image.html', filename=None)
|
||||
|
||||
@app.route('/uploads/<filename>')
|
||||
def uploaded_file(filename):
|
||||
return redirect(url_for('static', filename='uploads/' + filename))
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True)
|
||||
|
35
flask/templates/base.html
Normal file
35
flask/templates/base.html
Normal file
|
@ -0,0 +1,35 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Image Upload</title>
|
||||
|
||||
<style>
|
||||
.container {
|
||||
display: flex;
|
||||
width: 100%;
|
||||
}
|
||||
.box {
|
||||
flex: 1;
|
||||
background-color: lightgray;
|
||||
text-align: center;
|
||||
margin: 1px;
|
||||
}
|
||||
</style>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="box">Text</div>
|
||||
<div class="box">Image</div>
|
||||
<div class="box">QR</div>
|
||||
<div class="box">Barcode</div>
|
||||
</div>
|
||||
|
||||
{% block content %}
|
||||
{% endblock %}
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
19
flask/templates/image.html
Normal file
19
flask/templates/image.html
Normal file
|
@ -0,0 +1,19 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<h1>Upload Image</h1>
|
||||
<form method="POST" enctype="multipart/form-data">
|
||||
<input type="file" name="image">
|
||||
<input type="submit" value="Upload">
|
||||
</form>
|
||||
{% if filename %}
|
||||
<h2>Uploaded Image:</h2>
|
||||
<img src="{{ url_for('uploaded_file', filename=filename) }}" alt="Uploaded Image" style="max-width:300px;">
|
||||
{% if valid %}
|
||||
<p style="color: green;">Height: {{ height }} px</p>
|
||||
{% else %}
|
||||
<p style="color: red;">Height: {{ height }} px</p>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
Loading…
Reference in a new issue