Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pyqrcode | |
| from PIL import Image, ImageDraw, ImageFont | |
| import io | |
| from PIL import Image, ImageDraw, ImageFont | |
| import io | |
| import pyqrcode | |
| def generate_qr_code(url, title): | |
| # Generate QR code | |
| qr = pyqrcode.create(url) | |
| buffer = io.BytesIO() | |
| qr.png(buffer, scale=10) | |
| buffer.seek(0) | |
| qr_image = Image.open(buffer) | |
| image_width = qr_image.width | |
| image_height = qr_image.height + 100 # add space for text | |
| result_image = Image.new('RGB', (image_width, image_height), 'white') | |
| result_image.paste(qr_image, (0, 100)) | |
| draw = ImageDraw.Draw(result_image) | |
| font = ImageFont.truetype("dejavu-sans-bold.ttf", 24) | |
| # Here we use `textsize` | |
| text_width, text_height = draw.textsize(title, font=font) | |
| text_x = (image_width - text_width) // 2 | |
| text_y = 30 # Adjust vertical position | |
| draw.text((text_x, text_y), title, fill="black", font=font) | |
| return result_image | |
| # Create the Gradio interface | |
| iface = gr.Interface( | |
| fn=generate_qr_code, | |
| inputs=[ | |
| gr.Textbox(label="Enter URL", placeholder="Type or paste URL here..."), | |
| gr.Textbox(label="Enter Title for QR Code", placeholder="Type the title here...") | |
| ], | |
| outputs=gr.Image(label="QR Code Image", type="pil", format="png"), | |
| title="QR Code Generator", | |
| description="Enter a URL and a title to generate a QR Code. The title and the QR Code will be displayed in the same image." | |
| ) | |
| iface.launch() | |