Update app.py
Browse files
app.py
CHANGED
@@ -3,39 +3,38 @@ import pyqrcode
|
|
3 |
from PIL import Image, ImageDraw, ImageFont
|
4 |
import io
|
5 |
|
6 |
-
from PIL import Image, ImageDraw, ImageFont
|
7 |
-
import io
|
8 |
-
import pyqrcode
|
9 |
-
|
10 |
def generate_qr_code(url, title):
|
11 |
# Generate QR code
|
12 |
qr = pyqrcode.create(url)
|
13 |
buffer = io.BytesIO()
|
|
|
|
|
14 |
qr.png(buffer, scale=10)
|
15 |
|
|
|
16 |
buffer.seek(0)
|
17 |
qr_image = Image.open(buffer)
|
18 |
|
|
|
19 |
image_width = qr_image.width
|
20 |
-
image_height = qr_image.height +
|
21 |
-
|
|
|
22 |
result_image = Image.new('RGB', (image_width, image_height), 'white')
|
23 |
-
|
24 |
-
|
|
|
|
|
25 |
draw = ImageDraw.Draw(result_image)
|
26 |
-
|
27 |
-
|
28 |
-
# Here we use `textsize`
|
29 |
text_width, text_height = draw.textsize(title, font=font)
|
30 |
-
|
31 |
-
text_x
|
32 |
-
|
33 |
-
|
34 |
-
draw.text((text_x, text_y), title, fill="black", font=font)
|
35 |
-
|
36 |
return result_image
|
37 |
|
38 |
-
|
39 |
# Create the Gradio interface
|
40 |
iface = gr.Interface(
|
41 |
fn=generate_qr_code,
|
|
|
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,
|