summaryrefslogtreecommitdiff
path: root/megapixels/commands/processor/mirror.py
blob: 9ca1cac76347e7b25684b2128239c97b931c319d (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
"""
Crop images to prepare for training
"""

import click
import cv2 as cv
from PIL import Image, ImageOps, ImageFilter

from app.settings import types
from app.utils import click_utils
from app.settings import app_cfg as cfg


@click.command()
@click.option('-i', '--input', 'opt_dir_in', required=True,
  help='Input directory')
@click.option('-o', '--output', 'opt_dir_out', required=True,
  help='Output directory')
@click.option('--slice', 'opt_slice', type=(int, int), default=(None, None),
  help='Slice the input list')
@click.pass_context
def cli(ctx, opt_dir_in, opt_dir_out,  opt_slice):
  """Mirror augment image directory"""
  
  import os
  from os.path import join
  from pathlib import Path
  from glob import glob
  from tqdm import tqdm

  from app.utils import logger_utils, file_utils, im_utils
  
  # -------------------------------------------------
  # init

  log = logger_utils.Logger.getLogger()
  
  # -------------------------------------------------
  # process here

  # get list of files to process
  fp_ims = glob(join(opt_dir_in, '*.jpg'))
  fp_ims += glob(join(opt_dir_in, '*.png'))
  
  if opt_slice:
    fp_ims = fp_ims[opt_slice[0]:opt_slice[1]]
  log.info('processing {:,} files'.format(len(fp_ims)))
  
  # ensure output dir exists
  file_utils.mkdirs(opt_dir_out)

  # resize and save images
  for fp_im in tqdm(fp_ims):
    im = Image.open(fp_im)
    fpp_im = Path(fp_im)
    fp_out = join(opt_dir_out, '{}_mirror{}'.format(fpp_im.stem, fpp_im.suffix))
    im.save(fp_out)