Update app.py
Browse files
app.py
CHANGED
@@ -3,37 +3,45 @@ 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 |
qr.png(buffer, scale=10)
|
|
|
|
|
11 |
buffer.seek(0)
|
12 |
qr_image = Image.open(buffer)
|
13 |
-
|
14 |
# Prepare to add title to the image
|
15 |
image_width = qr_image.width
|
16 |
-
image_height = qr_image.height + 70 #
|
17 |
-
|
18 |
# Create a new image with white background
|
19 |
result_image = Image.new('RGB', (image_width, image_height), 'white')
|
|
|
20 |
result_image.paste(qr_image, (0, 70))
|
21 |
-
|
22 |
-
#
|
23 |
draw = ImageDraw.Draw(result_image)
|
24 |
-
font = ImageFont.truetype("dejavu-sans-bold.ttf", 24)
|
25 |
-
|
26 |
# Correctly using textsize to get dimensions of the text
|
27 |
-
text_width, text_height =
|
28 |
-
|
29 |
# Calculate the text's position (centered)
|
30 |
text_x = (image_width - text_width) // 2
|
31 |
text_y = 20 # Adjust as necessary
|
32 |
draw.text((text_x, text_y), title, font=font, fill='black')
|
33 |
-
|
34 |
return result_image
|
35 |
|
36 |
|
|
|
37 |
# Create the Gradio interface
|
38 |
iface = gr.Interface(
|
39 |
fn=generate_qr_code,
|
|
|
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 |
+
# Move to the beginning of the StringIO buffer
|
17 |
buffer.seek(0)
|
18 |
qr_image = Image.open(buffer)
|
19 |
+
|
20 |
# Prepare to add title to the image
|
21 |
image_width = qr_image.width
|
22 |
+
image_height = qr_image.height + 70 # Add 70 pixels space for title
|
23 |
+
|
24 |
# Create a new image with white background
|
25 |
result_image = Image.new('RGB', (image_width, image_height), 'white')
|
26 |
+
# Paste the QR code onto this new image
|
27 |
result_image.paste(qr_image, (0, 70))
|
28 |
+
|
29 |
+
# Draw the title on the image
|
30 |
draw = ImageDraw.Draw(result_image)
|
31 |
+
font = ImageFont.truetype("dejavu-sans-bold.ttf", 24) # Ensure your font path is correct
|
32 |
+
|
33 |
# Correctly using textsize to get dimensions of the text
|
34 |
+
text_width, text_height = draw.textsize(title, font=font)
|
35 |
+
|
36 |
# Calculate the text's position (centered)
|
37 |
text_x = (image_width - text_width) // 2
|
38 |
text_y = 20 # Adjust as necessary
|
39 |
draw.text((text_x, text_y), title, font=font, fill='black')
|
40 |
+
|
41 |
return result_image
|
42 |
|
43 |
|
44 |
+
|
45 |
# Create the Gradio interface
|
46 |
iface = gr.Interface(
|
47 |
fn=generate_qr_code,
|