from PIL import Image

SRC = '/root/.hermes/webui/attachments/9710c799d486/'
man_src = Image.open(SRC + '707f2402-9eb3-4aa0-a58b-fbb46d61815f.jpg')
woman_src = Image.open(SRC + 'e461f313-7e7d-4183-93b2-f3d0e9756d73.jpg')

# Full source is 946x2046
# Photo region: y=250 to y=1180 (the actual photo, excluding Instagram UI)
man_photo = man_src.crop((0, 250, 946, 1180))   # 946x930
woman_photo = woman_src.crop((0, 250, 946, 1180)) # 946x930

# Face center positions in the 946x930 photos
man_cx = 730
woman_cx = 512

TARGET_H = 800
TARGET_W = 640

def fit(im, w, h):
    return im.resize((w, h), Image.LANCZOS)

# Crop each face region generously and centered
m_fc = man_photo.crop((400, 240, 946, 830))    # center on x=727, y spans 240..830
w_fc = woman_photo.crop((100, 60, 870, 740))   # center on x=485, y spans 60..740

# Scale to target
mf = fit(m_fc, TARGET_W, TARGET_H)
wf = fit(w_fc, TARGET_W, TARGET_H)

# Split at nose bridge level (~40% from top)
split_y = round(TARGET_H * 0.40)
print(f'Split at y={split_y} out of {TARGET_H}')
print(f'Man face: {mf.size}, Woman face: {wf.size}')

# === VERSION A: Woman TOP | Man BOTTOM ===
w_top = wf.crop((0, 0, TARGET_W, split_y))
m_bot = mf.crop((0, split_y, TARGET_W, TARGET_H))
vA = Image.new('RGB', (TARGET_W, TARGET_H), (30, 30, 30))
vA.paste(w_top, (0, 0))
vA.paste(m_bot, (0, split_y))
vA.save('/root/workspace/split_headshot_A.jpg', quality=95)

# === VERSION B: Man TOP | Woman BOTTOM ===
m_top = mf.crop((0, 0, TARGET_W, split_y))
w_bot = wf.crop((0, split_y, TARGET_W, TARGET_H))
vB = Image.new('RGB', (TARGET_W, TARGET_H), (30, 30, 30))
vB.paste(m_top, (0, 0))
vB.paste(w_bot, (0, split_y))
vB.save('/root/workspace/split_headshot_B.jpg', quality=95)

print(f'A (woman top | man bottom): {vA.size}')
print(f'B (man top | woman bottom): {vB.size}')
