from PIL import Image

SRC = '/root/.hermes/webui/attachments/9710c799d486/'
man = Image.open(SRC + '707f2402-9eb3-4aa0-a58b-fbb46d61815f.jpg').crop((0, 250, 946, 1180))
woman = Image.open(SRC + 'e461f313-7e7d-4183-93b2-f3d0e9756d73.jpg').crop((0, 250, 946, 1180))

# Face centers in the 946x930 crops
man_cx = 730    # man's face is on the right side of the image
woman_cx = 512  # woman's face is near the center

TARGET_H = 960
HW = 280

def fit_h(im, h):
    w = round(im.width * h / im.height)
    return im.resize((w, h), Image.LANCZOS)

# ============================================================
# SPLIT-FACE LOGIC
# ============================================================
# Person facing camera:
#   anatomical RIGHT face = image LEFT of centerline
#   anatomical LEFT  face = image RIGHT of centerline
#
# Standard split-face: right half of person on LEFT of composite, left half on RIGHT.
# Two-person split: each person's nose points toward the center seam.
#
# Version A: Man LEFT | Woman RIGHT
#   Man:   his anatomical RIGHT (image LEFT of cx=730) → LEFT of composite → faces right/toward center
#   Woman: her anatomical LEFT  (image RIGHT of cx=512) → RIGHT of composite → faces left/toward center
# 
# Version B: Woman LEFT | Man RIGHT
#   Woman: her anatomical RIGHT (image LEFT of cx=512) → LEFT of composite → faces right/toward center  
#   Man:   his anatomical LEFT  (image RIGHT of cx=730) → RIGHT of composite → faces left/toward center
# ============================================================

def half(img, cx, y0, y1, side):
    """side='left' → image-left of center (anatomical right); side='right' → image-right (anatomical left)"""
    if side == 'left':
        return img.crop((max(0, cx - HW), y0, cx, y1))
    else:
        return img.crop((cx, y0, min(img.width, cx + HW), y1))

# === VERSION A: Man LEFT, Woman RIGHT ===
m_right = half(man, man_cx, 220, 930, 'left')     # man's anatomical right, faces right/toward center
w_left  = half(woman, woman_cx, 80, 740, 'right')   # woman's anatomical left, faces left/toward center

vA_l = fit_h(m_right, TARGET_H)
vA_r = fit_h(w_left, TARGET_H)
vA = Image.new('RGB', (vA_l.width + vA_r.width, TARGET_H), (30, 30, 30))
vA.paste(vA_l, (0, 0))
vA.paste(vA_r, (vA_l.width, 0))
vA.save('/root/workspace/split_headshot_A.jpg', quality=95)

# === VERSION B: Woman LEFT, Man RIGHT ===
w_right = half(woman, woman_cx, 80, 740, 'left')    # woman's anatomical right, faces right/toward center
m_left  = half(man, man_cx, 220, 930, 'right')      # man's anatomical left, faces left/toward center

vB_l = fit_h(w_right, TARGET_H)
vB_r = fit_h(m_left, TARGET_H)
vB = Image.new('RGB', (vB_l.width + vB_r.width, TARGET_H), (30, 30, 30))
vB.paste(vB_l, (0, 0))
vB.paste(vB_r, (vB_l.width, 0))
vB.save('/root/workspace/split_headshot_B.jpg', quality=95)

print(f'A: man-left={m_right.size} woman-right={w_left.size} composite={vA.size}')
print(f'B: woman-left={w_right.size} man-right={m_left.size} composite={vB.size}')