Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pyqrcode
|
3 |
+
from PIL import Image, ImageDraw, ImageFont
|
4 |
+
import io
|
5 |
+
|
6 |
+
def generate_qr_code(url, title):
|
7 |
+
# Generate QR code
|
8 |
+
qr = pyqrcode.create(url)
|
9 |
+
buffer = io.BytesIO()
|
10 |
+
|
11 |
+
# Create the QR code as a PNG
|
12 |
+
qr.png(buffer, scale=10)
|
13 |
+
|
14 |
+
# Move to the beginning of the StringIO buffer
|
15 |
+
buffer.seek(0)
|
16 |
+
qr_image = Image.open(buffer)
|
17 |
+
|
18 |
+
# Prepare to add title to the image
|
19 |
+
image_width = qr_image.width
|
20 |
+
image_height = qr_image.height + 70 # Add 70 pixels space for title
|
21 |
+
|
22 |
+
# Create a new image with white background
|
23 |
+
result_image = Image.new('RGB', (image_width, image_height), 'white')
|
24 |
+
# Paste the QR code onto this new image
|
25 |
+
result_image.paste(qr_image, (0, 70))
|
26 |
+
|
27 |
+
# Draw the title on the image
|
28 |
+
draw = ImageDraw.Draw(result_image)
|
29 |
+
font_path = "dejavu-sans-bold.ttf" # Ensure the font path is correct
|
30 |
+
font = ImageFont.truetype(font_path, 24) # Use a specific font size
|
31 |
+
text_width, text_height = draw.textsize(title, font=font)
|
32 |
+
text_x = (image_width - text_width) / 2 # Center the text
|
33 |
+
draw.text((text_x, 20), title, fill="black", font=font)
|
34 |
+
|
35 |
+
# Return the final image
|
36 |
+
return result_image
|
37 |
+
|
38 |
+
# Create the Gradio interface
|
39 |
+
iface = gr.Interface(
|
40 |
+
fn=generate_qr_code,
|
41 |
+
inputs=[
|
42 |
+
gr.Textbox(label="Enter URL", placeholder="Type or paste URL here..."),
|
43 |
+
gr.Textbox(label="Enter Title for QR Code", placeholder="Type the title here...")
|
44 |
+
],
|
45 |
+
outputs=gr.Image(label="QR Code Image", type="pil", format="png"),
|
46 |
+
title="QR Code Generator",
|
47 |
+
description="Enter a URL and a title to generate a QR Code. The title and the QR Code will be displayed in the same image."
|
48 |
+
)
|
49 |
+
|
50 |
+
iface.launch()
|