summaryrefslogtreecommitdiff
path: root/crop_frames.py
diff options
context:
space:
mode:
Diffstat (limited to 'crop_frames.py')
-rw-r--r--crop_frames.py15
1 files changed, 11 insertions, 4 deletions
diff --git a/crop_frames.py b/crop_frames.py
index c22adb4..b507a1f 100644
--- a/crop_frames.py
+++ b/crop_frames.py
@@ -9,6 +9,7 @@ from PIL import Image
parser = argparse.ArgumentParser()
parser.add_argument('--step', default=256, type=int)
parser.add_argument('--overlap', default=2, type=int)
+parser.add_argument('--ratio', default=2, type=int)
parser.add_argument('--in_dir', help='Directory to process')
parser.add_argument('--out_dir', default='inputs', help='Directory to output to')
args = parser.parse_args()
@@ -21,7 +22,13 @@ def crop_dir():
# print(files)
img = Image.open(files[0])
- width, height = img.size
+ width, old_height = img.size
+ if ratio == (width / height):
+ old_height = height
+ else:
+ height = round(width / ratio)
+ top_offset = round((old_height - height) / 2)
+
print("{}x{} {}".format(width, height, step))
for x in range(0, width, step):
for y in range(0, height, step):
@@ -36,13 +43,13 @@ def crop_dir():
dataset = []
for raw_fn in files:
- dataset.append((raw_fn, width, height, step, side, overlap,))
+ dataset.append((raw_fn, width, height, step, side, overlap, top_offset,))
chunksize = 3
with Pool(processes=cpu_count()) as pool:
pool.starmap(crop_image, dataset, chunksize)
-def crop_image(raw_fn, width, height, step, side, overlap):
+def crop_image(raw_fn, width, height, step, side, overlap, top_offset):
img = Image.open(raw_fn)
fn = os.path.basename(raw_fn)
for x in range(0, width, step):
@@ -53,7 +60,7 @@ def crop_image(raw_fn, width, height, step, side, overlap):
h = min(side, height - yy)
crop_dir = "{}/crop_{}_{}_{}_{}".format(args.out_dir, x, y, w, h)
# print("{}x{} {}x{}".format(x, y, w, h))
- crop = img.crop((xx, yy, xx + w, yy + h))
+ crop = img.crop((xx, yy + top_offset, xx + w, yy + h + top_offset))
crop.save("{}/{}".format(crop_dir, fn))
if __name__ == "__main__":