diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-11-29 20:21:23 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-11-29 20:21:23 +0100 |
| commit | 572a1687e3435719c64c9189367d4e24c88bda6e (patch) | |
| tree | fb09a7c371e831b1303ca518bf30d0cdd215b139 | |
| parent | d6532455b52f6ee6c4ef0c29242f1ee8ac5943e5 (diff) | |
script to crop equirectangular images with overlap
| -rw-r--r-- | crop-equirectangular.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/crop-equirectangular.py b/crop-equirectangular.py new file mode 100644 index 0000000..683cba8 --- /dev/null +++ b/crop-equirectangular.py @@ -0,0 +1,55 @@ +import os +import glob +import requests +import time +from PIL import Image + +# This script should be run once to generate crops with a specific aspect ratio from a pano. + +parser = argparse.ArgumentParser() +parser.add_argument('--folder', default="sequences/venice_360/equi") +parser.add_argument('--label', required=True) +parser.add_argument('--vertical_offset', type=int, default=983) +parser.add_argument('--overlap', type=float, default=0.5) +opt = parser.parse_args() + +src_width = 4096 +src_height = 2048 + +count = 3 +output_aspect = 3 + +overall_aspect = count * output_aspect +overlapped_aspect = overall_aspect - count * opt.overlap +crop_width = src_width * output_aspect / overlapped_aspect +crop_height = crop_width / output_aspect + +c0 = 7/6 * crop_width +c1 = 3/6 * crop_width +c2 = 5/6 * crop_width + +x0 = c0 - crop_width / 2 +x1 = c1 - crop_width / 2 +x2 = c2 - crop_width / 2 + +y0 = opt.vertical_offset - crop_height / 2 + +p0 = (x0, y0, x0 + crop_width, y0 + crop_height,) +p1 = (x1, y0, x1 + crop_width, y0 + crop_height,) +p2 = (x2, y0, x2 + crop_width, y0 + crop_height,) + +path_0 = os.path.join("sequences", label + "_a") +path_1 = os.path.join("sequences", label + "_b") +path_2 = os.path.join("sequences", label + "_c") + +for i, fn in enumerate(glob(os.path.join(opt.folder, '*.png'))): + fn = "frame_{:04d}.png".format(i) + + canvas = Image.new('RGB', (src_width, src_height * 3/2,)) + image = Image.open(fn) + canvas.paste(image, (0, 0)) + canvas.paste(image, (src_width, 0)) + + canvas.crop(p0).resize((1024, 512,), Image.ANTIALIAS).save(os.path.join(path_0, fn)) + canvas.crop(p1).resize((1024, 512,), Image.ANTIALIAS).save(os.path.join(path_1, fn)) + canvas.crop(p2).resize((1024, 512,), Image.ANTIALIAS).save(os.path.join(path_2, fn)) |
