Spaces:
Sleeping
Sleeping
Kvikontent
commited on
Commit
•
e08f6b5
1
Parent(s):
6d79b12
Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,15 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import qrcode
|
4 |
import io
|
5 |
-
from PIL import Image
|
|
|
6 |
import os
|
7 |
|
8 |
# Get the Hugging Face API token from the environment variable
|
9 |
hf_token = os.environ.get("hf_token")
|
10 |
|
11 |
-
# Function to generate a QR code
|
12 |
-
def
|
13 |
qr = qrcode.QRCode(
|
14 |
version=1,
|
15 |
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
@@ -20,7 +21,20 @@ def generate_qr_code(url):
|
|
20 |
qr.make(fit=True)
|
21 |
|
22 |
qr_img = qr.make_image(fill_color="black", back_color="white")
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Function to query the image from the Hugging Face API
|
26 |
def query_image(text):
|
@@ -32,17 +46,21 @@ def query_image(text):
|
|
32 |
|
33 |
# Gradio app function
|
34 |
def generate_image(url, text):
|
35 |
-
# Generate QR code
|
36 |
-
qr_img = generate_qr_code(url)
|
37 |
-
|
38 |
# Generate image from Hugging Face API
|
39 |
image_bytes = query_image(text)
|
40 |
api_image = Image.open(io.BytesIO(image_bytes))
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
return final_image
|
48 |
|
|
|
2 |
import requests
|
3 |
import qrcode
|
4 |
import io
|
5 |
+
from PIL import Image, ImageDraw
|
6 |
+
|
7 |
import os
|
8 |
|
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,
|
|
|
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):
|
|
|
46 |
|
47 |
# Gradio app function
|
48 |
def generate_image(url, text):
|
|
|
|
|
|
|
49 |
# Generate image from Hugging Face API
|
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 |
|