Saditiya commited on
Commit
18ff94e
·
verified ·
1 Parent(s): 0ad7bba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -33
app.py CHANGED
@@ -1,46 +1,51 @@
1
  import gradio as gr
2
  import qrcode
 
 
 
3
  from PIL import Image
4
-
5
- def generate_qr(data, logo_file, bg_file):
6
- # Buat QR dasar
 
 
 
 
 
 
 
 
 
7
  qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
8
- qr.add_data(data)
9
  qr.make(fit=True)
 
10
 
11
- qr_img = qr.make_image(fill_color="black", back_color="white").convert("RGBA")
12
-
13
- # Tambahkan background jika ada
14
- if bg_file is not None:
15
- bg = Image.open(bg_file).convert("RGBA")
16
- bg = bg.resize(qr_img.size)
17
- qr_img = Image.alpha_composite(bg, qr_img)
18
 
19
- # Tambahkan logo jika ada
20
- if logo_file is not None:
21
- logo = Image.open(logo_file).convert("RGBA")
22
- # Skala logo agar tidak terlalu besar (misalnya 20% dari lebar QR)
23
- logo_size = int(qr_img.size[0] * 0.2)
24
- logo = logo.resize((logo_size, logo_size))
25
- pos = ((qr_img.size[0] - logo_size) // 2, (qr_img.size[1] - logo_size) // 2)
26
- qr_img.paste(logo, pos, mask=logo)
 
27
 
28
- return qr_img
29
 
30
  with gr.Blocks() as demo:
31
- gr.Markdown("## Custom QR Code Generator with Logo & Background")
32
-
33
  with gr.Row():
34
- data = gr.Textbox(label="Link / Text untuk QR", placeholder="https://contoh.com")
35
-
36
- with gr.Row():
37
- logo_file = gr.File(label="Upload Logo (PNG)" , type="filepath")
38
- bg_file = gr.File(label="Upload Background (PNG/JPG)", type="filepath")
39
-
40
- generate_btn = gr.Button("Generate QR")
41
- output_img = gr.Image(label="Hasil QR Code")
42
 
43
- generate_btn.click(fn=generate_qr, inputs=[data, logo_file, bg_file], outputs=output_img)
44
 
45
- if __name__ == "__main__":
46
- demo.launch()
 
1
  import gradio as gr
2
  import qrcode
3
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
4
+ from diffusers.utils import load_image
5
+ import torch
6
  from PIL import Image
7
+ import io
8
+
9
+ # Load ControlNet model khusus QR
10
+ controlnet = ControlNetModel.from_pretrained(
11
+ "monster-labs/controlnet-qr-code", torch_dtype=torch.float16
12
+ )
13
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
14
+ "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
15
+ ).to("cuda")
16
+
17
+ def generate_qr_with_logo(text, image):
18
+ # 1. Buat QR code dasar
19
  qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
20
+ qr.add_data(text)
21
  qr.make(fit=True)
22
+ qr_img = qr.make_image(fill_color="black", back_color="white").convert("RGB")
23
 
24
+ # 2. Resize agar sama dengan gambar input
25
+ if image is not None:
26
+ image = image.resize((512, 512))
27
+ qr_img = qr_img.resize((512, 512))
 
 
 
28
 
29
+ # 3. Gabungkan: ControlNet akan memandu agar QR mengikuti gambar dasar
30
+ prompt = "artistic QR code blended with uploaded image, still scannable"
31
+ result = pipe(
32
+ prompt,
33
+ image=qr_img,
34
+ control_image=qr_img,
35
+ num_inference_steps=30,
36
+ guidance_scale=9.0
37
+ ).images[0]
38
 
39
+ return result
40
 
41
  with gr.Blocks() as demo:
42
+ gr.Markdown("## Artistic QR Code Generator (seperti Starbucks QR)")
 
43
  with gr.Row():
44
+ text = gr.Textbox(label="Teks/Link untuk QR code")
45
+ img = gr.Image(label="Upload gambar dasar (logo/foto)", type="pil")
46
+ btn = gr.Button("Generate")
47
+ output = gr.Image(label="QR Code Hasil")
 
 
 
 
48
 
49
+ btn.click(fn=generate_qr_with_logo, inputs=[text, img], outputs=output)
50
 
51
+ demo.launch()