Kvikontent commited on
Commit
ad200c9
1 Parent(s): e08f6b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -25
app.py CHANGED
@@ -9,8 +9,8 @@ import os
9
  # Get the Hugging Face API token from the environment variable
10
  hf_token = os.environ.get("hf_token")
11
 
12
- # Function to generate a QR code with transparency
13
- def generate_qr_image(url):
14
  qr = qrcode.QRCode(
15
  version=1,
16
  error_correction=qrcode.constants.ERROR_CORRECT_L,
@@ -20,21 +20,12 @@ def generate_qr_image(url):
20
  qr.add_data(url)
21
  qr.make(fit=True)
22
 
23
- qr_img = qr.make_image(fill_color="black", back_color="white")
24
- qr_img_rgba = qr_img.convert("RGBA")
25
-
26
- # Add transparency to the QR code
27
- qr_data = qr_img_rgba.getdata()
28
- new_qr_data = []
29
- for item in qr_data:
30
- if item[0] == 0 and item[1] == 0 and item[2] == 0:
31
- new_qr_data.append((0, 0, 0, 0))
32
- else:
33
- new_qr_data.append(item)
34
 
35
- qr_img_rgba.putdata(new_qr_data)
 
36
 
37
- return qr_img_rgba
38
 
39
  # Function to query the image from the Hugging Face API
40
  def query_image(text):
@@ -50,17 +41,11 @@ def generate_image(url, text):
50
  image_bytes = query_image(text)
51
  api_image = Image.open(io.BytesIO(image_bytes))
52
 
53
- # Generate QR code image with transparency
54
- qr_image = generate_qr_image(url)
55
-
56
- # Resize the QR code to fit into the generated image
57
- qr_width, qr_height = qr_image.size
58
- api_image_width, api_image_height = api_image.size
59
- qr_image = qr_image.resize((int(qr_width * 0.5), int(qr_height * 0.5)))
60
 
61
- # Blend the two images together
62
- final_image = api_image.copy()
63
- final_image.paste(qr_image, (api_image_width - qr_image.width, api_image_height - qr_image.height), qr_image)
64
 
65
  return final_image
66
 
 
9
  # Get the Hugging Face API token from the environment variable
10
  hf_token = os.environ.get("hf_token")
11
 
12
+ # Function to generate a QR code
13
+ def generate_qr_image(url, image_size):
14
  qr = qrcode.QRCode(
15
  version=1,
16
  error_correction=qrcode.constants.ERROR_CORRECT_L,
 
20
  qr.add_data(url)
21
  qr.make(fit=True)
22
 
23
+ qr_img = qr.make_image(fill_color="black", back_color="white").convert("RGBA")
 
 
 
 
 
 
 
 
 
 
24
 
25
+ # Resize the QR code to cover the entire image area
26
+ qr_img = qr_img.resize((image_size[0], image_size[1]))
27
 
28
+ return qr_img
29
 
30
  # Function to query the image from the Hugging Face API
31
  def query_image(text):
 
41
  image_bytes = query_image(text)
42
  api_image = Image.open(io.BytesIO(image_bytes))
43
 
44
+ # Generate QR code image
45
+ qr_image = generate_qr_image(url, api_image.size)
 
 
 
 
 
46
 
47
+ # Blend the API image and QR code
48
+ final_image = Image.alpha_composite(api_image, qr_image)
 
49
 
50
  return final_image
51