-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
135 lines (113 loc) · 5.21 KB
/
Copy pathmain.py
File metadata and controls
135 lines (113 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from flask import Flask, render_template_string, request
import base64
from PIL import Image
# for QR
import qrcode
app = Flask(__name__)
image_path = 'static/images/qr_code.png'
def resize_image(path, size):
base_width= size
img = Image.open(path)
wpercent = (base_width / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((base_width, hsize), Image.Resampling.LANCZOS)
img.save(path)
def generat_qr(payload):
# Generate QR code image
img = qrcode.make(payload)
# Save image to a file
img.save(image_path)
resize_image(image_path, 400)
@app.route('/', methods=['POST', 'GET'])
def home():
if request.method == 'POST':
generat_qr(request.form['data'])
# Open the image file and read its contents as bytes
with open(image_path, 'rb') as f:
img_bytes = f.read()
# Convert the image bytes to a base64 encoded string
encoded_img = base64.b64encode(img_bytes).decode('utf-8')
# Create the HTML template and pass in the base64 encoded image string
html_template = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Image Rendering UI</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twemoji/13.1.0/twemoji.min.css" integrity="sha512-JUbjgJNcAQ2XpUJQs5uJth6NVx+ejNB4pW0+V6JLsRl/1yKcNl+Z8c3Uyehsc14EwkW5QYU+tw1c8gW0ns96OQ==" crossorigin="anonymous" />
<style>
body{
height: 100vh;
}
.container {
height: 100%;
}
.qr-code-gif{
width: 250px;
height: 250px;
}
</style>
</head>
<body>
<div class="container container d-flex flex-column flex-md-row align-items-center justify-content-center">
<img class="img-fluid" src="data:image/jpeg;base64,{{ image }}" alt="your-image-description">
<h1 class="display-4 px-3">Here!! Your QR Code<span class="twemoji twemoji-32">😉</span> <br> <a href="/" class="btn btn-outline-info rounded-pill">Generate again</a>
</h1>
<br>
</div>
<!-- Bootstrap JavaScript -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
</html>
"""
return render_template_string(html_template, image=encoded_img)
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QR Generator</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twemoji/13.1.0/twemoji.min.css" integrity="sha512-JUbjgJNcAQ2XpUJQs5uJth6NVx+ejNB4pW0+V6JLsRl/1yKcNl+Z8c3Uyehsc14EwkW5QYU+tw1c8gW0ns96OQ==" crossorigin="anonymous" />
<style>
body{
height: 100vh;
}
.container {
height: 100%;
}
.qr-code-gif{
width: 250px;
height: 250px;
}
</style>
</head>
<body>
<div class="container d-flex flex-column flex-md-row align-items-center justify-content-center">
<img class="qr-code-gif mx-md-5" src="./static/images/qr-code.gif" alt="animated qr code gif">
<div class="">
<h1 class="">QR Generator</h1><br>
<form method="post" action="/">
<div class="mb-5">
<input class="form-control py-4 rounded-pill" name="data" type="text" placeholder="paste your entry here">
</div>
<input class="btn btn-outline-info rounded-pill" class="float: right;" type="submit" value="Generate">
</form>
</div>
</div>
<!-- Bootstrap JavaScript -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
</html>
"""
return render_template_string(html_template)
if __name__ == '__main__':
app.run()