import os import glob import requests import time from PIL import Image # This script generates crops with a specific aspect ratio from a 360 video. # It creates three sequences (identified by "--label") # The default is a 24 degree overlap (equivalent to 1/6 of the 3:1 output image) # Setting a higher overlap means you can have taller vertical FOV. parser = argparse.ArgumentParser() parser.add_argument('--folder', default="/media/ssd/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))