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
woman_cx = 512
TARGET_H = 960
HW = 260

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

def make_split(left_src, left_cx, left_y0, left_y1,
              right_src, right_cx, right_y0, right_y1, mirror_right=False):
    """
    LEFT half of output: LEFT half of left_src's face (image-left of centerline)
    RIGHT half of output: RIGHT half of right_src's face (image-right of centerline)
    Both anatomical LEFT face sides (facing viewer).
    The RIGHT half is flipped horizontally so both sides face toward viewer.
    """
    # Left source: image LEFT of centerline = anatomical RIGHT face half
    lr = left_src.crop((left_cx - HW, left_y0, left_cx, left_y1))
    # Right source: image RIGHT of centerline = anatomical LEFT face half
    rr = right_src.crop((right_cx, right_y0, right_cx + HW, right_y1))
    lh = fit_h(lr, TARGET_H)
    rh = fit_h(rr, TARGET_H)
    if mirror_right:
        rh = rh.transpose(Image.FLIP_LEFT_RIGHT)
    out = Image.new('RGB', (lh.width + rh.width, TARGET_H), (40, 40, 40))
    out.paste(lh, (0, 0))
    out.paste(rh, (lh.width, 0))
    return out

# Version A: man on LEFT, woman on RIGHT
# Man's anatomical left = image RIGHT of his centerline -> FLIP so it faces viewer
# Woman's anatomical left = image RIGHT of her centerline -> FLIP so it faces viewer
# Actually for both to face viewer:
#   left half shows man's anatomical right (image-left of centerline) - faces viewer naturally
#   right half shows woman's anatomical left (image-right of centerline) - faces viewer naturally, no flip needed

vA_region_l = man.crop((man_cx - HW, 220, man_cx, 930))     # man's anatomical right, faces viewer
vA_region_r = woman.crop((woman_cx, 80, woman_cx + HW, 740)) # woman's anatomical left, faces viewer
vA_l = fit_h(vA_region_l, TARGET_H)
vA_r = fit_h(vA_region_r, TARGET_H)
vA = Image.new('RGB', (vA_l.width + vA_r.width, TARGET_H), (40, 40, 40))
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 on LEFT, man on RIGHT
vB_region_l = woman.crop((woman_cx - HW, 80, woman_cx, 740))  # woman's anatomical right, faces viewer
vB_region_r = man.crop((man_cx, 220, man_cx + HW, 930))       # man's anatomical left, faces viewer
vB_l = fit_h(vB_region_l, TARGET_H)
vB_r = fit_h(vB_region_r, TARGET_H)
vB = Image.new('RGB', (vB_l.width + vB_r.width, TARGET_H), (40, 40, 40))
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('A', vA.size)
print('B', vB.size)
print('A left: man right-face [%d..%d]' % (man_cx-HW, man_cx))
print('A right: woman left-face [%d..%d]' % (woman_cx, woman_cx+HW))
print('B left: woman right-face [%d..%d]' % (woman_cx-HW, woman_cx))
print('B right: man left-face [%d..%d]' % (man_cx, man_cx+HW))
