MK-316 commited on
Commit
4cd2cb0
1 Parent(s): bb02b59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -18
app.py CHANGED
@@ -4,37 +4,36 @@ 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,
 
4
  import io
5
 
6
  def generate_qr_code(url, title):
7
+ # Generate QR code using pyqrcode
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 # Additional space for title
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
+ # Prepare to draw the title
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 = font.getsize(title)
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,