summaryrefslogtreecommitdiff
path: root/crop-equirectangular.py
blob: d4f90f771c543a0a2d879d25423d1f9e05f791ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import glob
import argparse
from shutil import rmtree
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 * src_width
c1 = 3/6 * src_width
c2 = 5/6 * src_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", opt.label + "_a")
path_1 = os.path.join("sequences", opt.label + "_b")
path_2 = os.path.join("sequences", opt.label + "_c")

if os.path.exists(path_0):
  rmtree(path_0)
if os.path.exists(path_1):
  rmtree(path_1)
if os.path.exists(path_2):
  rmtree(path_2)

os.makedirs(path_0)
os.makedirs(path_1)
os.makedirs(path_2)

for i, fn in enumerate(glob.glob(os.path.join(opt.folder, '*.png'))):
  if (i % 100) == 0:
    print("{}...".format(i))
  out_fn = "frame_{:05d}.png".format(i + 1)

  canvas = Image.new('RGB', (int(src_width * 3/2), src_height,))
  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, out_fn))
  canvas.crop(p1).resize((1024, 512,), Image.ANTIALIAS).save(os.path.join(path_1, out_fn))
  canvas.crop(p2).resize((1024, 512,), Image.ANTIALIAS).save(os.path.join(path_2, out_fn))