summaryrefslogtreecommitdiff
path: root/crop-margin.py
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2019-01-09 23:12:19 +0100
committerJules Laplace <julescarbon@gmail.com>2019-01-09 23:12:19 +0100
commit333a743e278dac3f0997bad1b6480ecbceb1d71c (patch)
tree854c2b5ecedb0cf495526e96b1796a8674d76cb0 /crop-margin.py
parentfd022cafdc8a782e00bdb91fb2afb3f4d7893fec (diff)
uprez thirds
Diffstat (limited to 'crop-margin.py')
-rw-r--r--crop-margin.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/crop-margin.py b/crop-margin.py
new file mode 100644
index 0000000..5183258
--- /dev/null
+++ b/crop-margin.py
@@ -0,0 +1,70 @@
+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="./uprez/venice_360_equi_1024/a/")
+parser.add_argument('--out_dir', default="./uprez_results/")
+parser.add_argument('--label', 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)
+dst_size = (dst_width, dst_height,)
+y0 = opt.vertical_offset - dst_height / 2 - margin
+
+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 * dst_width) - margin
+ if x < 0:
+ x += dst_width
+ crops.append((x, y0, x + dst_width + margin + margin, y0 + dst_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))
+
+ canvas = Image.new('RGB', (int(src_width + dst_width + margin), src_height,))
+ image = Image.open(fn)
+ canvas.paste(image, (0, 0))
+ canvas.paste(image, (src_width, 0))
+
+ for n, path in enumerate(paths):
+ image.crop(crops[n]).save(os.path.join(path, out_fn))
+
+chunksize = 3
+with Pool(processes=cpu_count()) as pool:
+ pool.starmap(build_thumbnail, dataset, chunksize)