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))

man_cx = 730    # his face center in the 946-wide image (his face is on the right)
woman_cx = 512  # her face center (near middle)
TARGET_H = 960
HW = 280        # half-face width

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

def split_face(img_left, left_cx, left_y0, left_y1,
               img_right, right_cx, right_y0, right_y1, invert_right=False):
    """
    LEFT of output  = LEFT anatomical face side of img_left  (image LEFT of its centerline)
    RIGHT of output = LEFT anatomical face side of img_right (image RIGHT of its centerline)
    Both halves face toward viewer (both their left sides).
    """
    # Left source: take region LEFT of centerline (anatomical right face)
    lr = img_left.crop((left_cx - HW, left_y0, left_cx, left_y1))
    # Right source: take region RIGHT of centerline (anatomical left face)
    rr = img_right.crop((right_cx, right_y0, right_cx + HW, right_y1))
    lh = fit_h(lr, TARGET_H)
    rh = fit_h(rr, TARGET_H)
    if invert_right:
        rh = rh.transpose(Image.FLIP_LEFT_RIGHT)
    out = Image.new('RGB', (lh.width + rh.width, TARGET_H), (30, 30, 30))
    out.paste(lh, (0, 0))
    out.paste(rh, (lh.width, 0))
    return out

# Version A: man on LEFT half, woman on RIGHT half
# man's anatomical LEFT face side = image RIGHT of his centerline (cx=730)
# woman's anatomical RIGHT face side = image LEFT of her centerline (cx=512)
vA = split_face(man, 730, 220, 930, woman, 512, 80, 740, invert_right=True)
vA.save('/root/workspace/split_headshot_A.jpg', quality=95)

# Version B: woman on LEFT half, man on RIGHT half
vB = split_face(woman, 512, 80, 740, man, 730, 220, 930, invert_right=True)
vB.save('/root/workspace/split_headshot_B.jpg', quality=95)

print('A size:', vA.size)
print('B size:', vB.size)
