MK-316 commited on
Commit
60f4fc6
1 Parent(s): 8319bcd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -18
app.py CHANGED
@@ -13,33 +13,27 @@ def generate_qr_code(url, title):
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
 
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 + 100 # add space for text
21
+
 
22
  result_image = Image.new('RGB', (image_width, image_height), 'white')
23
+ result_image.paste(qr_image, (0, 100))
24
+
 
 
25
  draw = ImageDraw.Draw(result_image)
26
+ font = ImageFont.truetype("dejavu-sans-bold.ttf", 24)
27
+
28
+ # Here we use `textsize`
29
  text_width, text_height = draw.textsize(title, font=font)
30
+
 
31
  text_x = (image_width - text_width) // 2
32
+ text_y = 30 # Adjust vertical position
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