hmb HF Staff commited on
Commit
9069a89
Β·
1 Parent(s): bbb1286

Cover-crop images to fill stamp area regardless of aspect ratio

Browse files
Files changed (1) hide show
  1. app.py +13 -4
app.py CHANGED
@@ -96,15 +96,23 @@ def build_stamp(arr, color_name, style, label):
96
  aw, ah = W - 2*BRD, H - 2*BRD - LH - 8
97
 
98
  photo = Image.fromarray(arr).convert("RGB")
99
- photo.thumbnail((aw, ah), Image.LANCZOS)
 
 
 
 
 
 
 
 
 
100
  photo = stylize(photo, style, hex_c)
101
 
102
  stamp = Image.new("RGBA", (W, H), (r, g, b, 255))
103
  draw = ImageDraw.Draw(stamp)
104
  draw.rectangle([BRD-4, BRD-4, W-BRD+3, H-BRD+3], fill=(255,255,255,255))
105
 
106
- px, py = BRD + (aw - photo.width)//2, BRD + 4
107
- stamp.paste(photo.convert("RGBA"), (px, py))
108
 
109
  if label.strip():
110
  font = load_font(11)
@@ -408,6 +416,7 @@ span.svelte-radiogroup,
408
  .color-pick .wrap label:nth-child(7)::before { background: #66BB6A !important; }
409
 
410
  /* ─── Radio: style pills ─── */
 
411
  .style-pick .wrap { display: flex !important; gap: 6px !important; flex-wrap: wrap !important; }
412
  .style-pick .wrap label {
413
  padding: 5px 14px !important;
@@ -569,7 +578,7 @@ SECTION_DIVIDER = """
569
  """
570
 
571
  CONTROLS_HEADER = """
572
- <div style="font-family:'Space Mono',monospace;font-size:.6rem;letter-spacing:.18em;text-transform:uppercase;color:#3D2E1E;margin-bottom:20px;margin-top:8px;">
573
  ── studio controls ──
574
  </div>
575
  """
 
96
  aw, ah = W - 2*BRD, H - 2*BRD - LH - 8
97
 
98
  photo = Image.fromarray(arr).convert("RGB")
99
+ # Cover crop: scale to fill awΓ—ah, then center crop
100
+ src_ratio = photo.width / photo.height
101
+ area_ratio = aw / ah
102
+ if src_ratio > area_ratio:
103
+ new_h, new_w = ah, int(photo.width * ah / photo.height)
104
+ else:
105
+ new_w, new_h = aw, int(photo.height * aw / photo.width)
106
+ photo = photo.resize((new_w, new_h), Image.LANCZOS)
107
+ left, top = (new_w - aw) // 2, (new_h - ah) // 2
108
+ photo = photo.crop((left, top, left + aw, top + ah))
109
  photo = stylize(photo, style, hex_c)
110
 
111
  stamp = Image.new("RGBA", (W, H), (r, g, b, 255))
112
  draw = ImageDraw.Draw(stamp)
113
  draw.rectangle([BRD-4, BRD-4, W-BRD+3, H-BRD+3], fill=(255,255,255,255))
114
 
115
+ stamp.paste(photo.convert("RGBA"), (BRD, BRD + 4))
 
116
 
117
  if label.strip():
118
  font = load_font(11)
 
416
  .color-pick .wrap label:nth-child(7)::before { background: #66BB6A !important; }
417
 
418
  /* ─── Radio: style pills ─── */
419
+ .style-pick { padding-bottom: 12px !important; }
420
  .style-pick .wrap { display: flex !important; gap: 6px !important; flex-wrap: wrap !important; }
421
  .style-pick .wrap label {
422
  padding: 5px 14px !important;
 
578
  """
579
 
580
  CONTROLS_HEADER = """
581
+ <div style="font-family:'Space Mono',monospace;font-size:.6rem;letter-spacing:.18em;text-transform:uppercase;color:#3D2E1E;margin-bottom:20px;margin-top:28px;">
582
  ── studio controls ──
583
  </div>
584
  """