MindCraft24729 commited on
Commit
23a32f1
·
verified ·
1 Parent(s): fa2f7ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -4
app.py CHANGED
@@ -24,17 +24,32 @@ def generate_image(prompt, model):
24
  response = requests.get(url, stream=True)
25
  response.raise_for_status()
26
 
27
- # Open main image
28
  img = Image.open(BytesIO(response.content)).convert("RGBA")
29
 
30
- # Paste watermark
31
- img.paste(watermark, (10, 10), mask=watermark)
 
 
 
32
 
33
- return img.convert("RGB")
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  except Exception as e:
36
  raise gr.Error(f"Unexpected error: {str(e)}")
37
 
 
38
  # ✅ Gradio UI with custom CSS
39
  with gr.Blocks(css="custom_hide_footer.css") as demo:
40
  gr.Markdown("## 🎨 CodeHubb AI Image Generator")
 
24
  response = requests.get(url, stream=True)
25
  response.raise_for_status()
26
 
27
+ # Load generated image
28
  img = Image.open(BytesIO(response.content)).convert("RGBA")
29
 
30
+ # Resize watermark to fixed width (e.g., 200px)
31
+ wm_width = 200
32
+ wm_ratio = wm_width / watermark.width
33
+ wm_size = (wm_width, int(watermark.height * wm_ratio))
34
+ resized_watermark = watermark.resize(wm_size)
35
 
36
+ # ✅ Paste watermark at top-left
37
+ img.paste(resized_watermark, (10, 10), resized_watermark)
38
+
39
+ # ✅ Convert to RGB and then to JPEG
40
+ final_img = img.convert("RGB")
41
+
42
+ # ✅ Save in-memory as JPEG (Gradio will return it)
43
+ buffer = BytesIO()
44
+ final_img.save(buffer, format="JPEG")
45
+ buffer.seek(0)
46
+
47
+ return Image.open(buffer)
48
 
49
  except Exception as e:
50
  raise gr.Error(f"Unexpected error: {str(e)}")
51
 
52
+
53
  # ✅ Gradio UI with custom CSS
54
  with gr.Blocks(css="custom_hide_footer.css") as demo:
55
  gr.Markdown("## 🎨 CodeHubb AI Image Generator")