crop.py 909 B

1234567891011121314151617
  1. #!/usr/bin/env python3
  2. """crop.py <id> <ours.png> <out.png> x y w h [scale] - same region (window coords) of the best WE frame and ours, side by side"""
  3. import glob, os, sys
  4. import numpy as np
  5. from PIL import Image
  6. id_, ours, out = sys.argv[1:4]
  7. x, y, w, h = map(int, sys.argv[4:8])
  8. k = int(sys.argv[8]) if len(sys.argv) > 8 else 2
  9. o = Image.open(ours).convert('RGB').crop((0, 0, 1916, 1050))
  10. fs = sorted(glob.glob(os.path.expanduser(f'~/.local/share/we_live/refs/{id_}/f*.png')))
  11. we = [Image.open(f).convert('RGB').crop((4, 30, 1920, 1080)) for f in fs]
  12. oa = np.asarray(o, np.float32)
  13. b = min(we, key=lambda im: np.abs(np.asarray(im, np.float32) - oa).mean())
  14. g = Image.new('RGB', (2 * w * k + 8, h * k), (255, 0, 255))
  15. g.paste(b.crop((x, y, x + w, y + h)).resize((w * k, h * k), Image.NEAREST), (0, 0))
  16. g.paste(o.crop((x, y, x + w, y + h)).resize((w * k, h * k), Image.NEAREST), (w * k + 8, 0))
  17. g.save(out)