summaryrefslogtreecommitdiff
path: root/crop-thirds.py
blob: f0bcf18f8ac2cec6f6c1013bd8011b3ca3e10f7a (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import os
import glob
import argparse
from shutil import rmtree
from PIL import Image
from multiprocessing import Pool, cpu_count
from dotenv import load_dotenv, find_dotenv
import subprocess
load_dotenv(find_dotenv())

# This script generates crops with a specific aspect ratio from a 360 video.
# It creates three sequences (identified by "--label")

parser = argparse.ArgumentParser()
parser.add_argument('--folder', default="./results/wood/")
parser.add_argument('--out_dir', default="./thirds/")
parser.add_argument('--label', required=True)
parser.add_argument('--vertical_offset', type=int, default=256)
# parser.add_argument('--dst_width', type=int, default=1024)
# parser.add_argument('--dst_height', type=int, default=512)
parser.add_argument('--count', type=int, default=3)
parser.add_argument('--max', type=int, default=99997)
parser.add_argument('--crop_aspect', type=float, default=1.5)
parser.add_argument('--aspect', type=float, default=3.0)
# parser.add_argument('--folder_id', type=int, required=True)
parser.add_argument('--margin', type=int, default=16)
parser.add_argument('--no_clobber', action='store_false')
opt = parser.parse_args()

src_width = 1024
src_height = 512

dst_width = int(src_width / opt.count)
dst_height = int(dst_width / opt.aspect)

crop_width = dst_width
crop_height = int(dst_width / opt.aspect * opt.crop_aspect)

x_margin = opt.margin
y_margin = opt.margin * opt.crop_aspect

crop_size = (crop_width, crop_height,)
dst_size = (dst_width + opt.margin * 2, dst_height + opt.margin * 2,)
y0 = opt.vertical_offset - crop_height / 2 - y_margin

crops = []
paths = []
out_path = os.path.join(opt.out_dir, opt.label)

if not opt.no_clobber:
  if os.path.exists(out_path):
    rmtree(out_path)

for i in range(opt.count):
  x = int(i / opt.count * src_width) - x_margin
  w = x + crop_width + x_margin + x_margin
  if w > src_width:
    w = src_width
    x = src_width - (crop_width + x_margin + x_margin)
  if x < 0:
    x = 0
  crops.append((x, y0, w, y0 + crop_height + margin + margin,))
  path = os.path.join(out_path, chr(97 + i))
  os.makedirs(path)
  paths.append(path)

max_i = opt.max
dataset = []
for i, fn in enumerate(sorted(glob.glob(os.path.join(opt.folder, '*.png')))):
  if i >= max_i:
    break
  out_fn = "frame_{:05d}.png".format(i + 1)
  if opt.no_clobber and os.path.exists(os.path.join(paths[0], out_fn)):
    continue
  dataset.append((i, fn,))

def build_thumbnail(i, fn):
  out_fn = "frame_{:05d}.png".format(i + 1)
  if (i % 100) == 0:
    print("{}...".format(i))

<<<<<<< HEAD
  canvas = Image.new('RGB', (int(src_width + dst_width + x_margin), src_height,))
=======
  # canvas = Image.new('RGB', (int(src_width + dst_width + margin), src_height,))
>>>>>>> 455c3fef555b6f556321c2db6a8dc355154666da
  image = Image.open(fn)

  for n, path in enumerate(paths):
    image.crop(crops[n]).resize(dst_size).save(os.path.join(path, out_fn))

chunksize = 3
with Pool(processes=cpu_count()) as pool:
  pool.starmap(build_thumbnail, dataset, chunksize)

# if opt.folder_id > 0:
#   endpoint = os.getenv('API_REMOTE') + '/api/file/'
#   for label in labels:
#     subprocess.call([
#       "curl",
#       "-X", "POST",
#       "-d", "folder_id={}".format(opt.folder_id),
#       "-d", "module=pix2pixhd",
#       "-d", "name={}.mov".format(label),
#       "-d", "url=https://s3.amazonaws.com/i.asdf.us/cortex/lens/data/{}/{}.mov".format(opt.folder_id, label),
#       "-d", "dataset={}".format(label),
#       "-d", "activity=splice",
#       "-d", "generated=0",
#       "-d", "processed=1",
#       "-d", "datatype=video",
#       endpoint
#     ])