def extra_files_by_postfix(ori_root, dest_root, match_postfix='', new_postfix=None, match_ext='*'): ''' extra files from ori_root to dest_root by match_postfix and match_ext params: ori_root: the dir of files that need to be processed dest_root: the dir for saving matched files match_postfix: the postfix to be matched new_postfix: the postfix for matched files default: None, that is keeping the ori postfix match_ext: the ext to be matched ''' if new_postfix is None: new_postfix = match_postfix handle_dir(dest_root) flag_img_list = glob_match( os.path.join(ori_root, "*{}.{}".format(match_postfix, match_ext))) for im in flag_img_list: fname, ext = get_fname_ext(im) dest_basename = '{}{}.{}'.format(fname[-len(match_postfix):], new_postfix, ext) src = im dst = os.path.join(dest_root, dest_basename) copy_file(src, dst)
def batch_shift_images(ori_root, dest_root, offset_x=0., offset_y=0., filename_template="{}.png"): # TODO ''' function: shifting images by (offset_x, offset_y) on (axis-x, axis-y) in batches params: ori_root: string, the dir of images that need to be processed dest_root: string, the dir to save processed images offset_x: float, offset pixels on axis-x positive=left; negative=right offset_y: float, offset pixels on axis-y positive=up; negative=down filename_template: string, the filename template for saving images ''' handle_dir(dest_root) offset_x, offset_y = float(offset_x), float(offset_y) images_fname = sorted(listdir(ori_root)) for imf in images_fname: img = cv2.imread(os.path.join(ori_root, imf)).astype('float32') img = image_shift(img, offset_x=offset_x, offset_y=offset_y) cv2.imwrite( os.path.join(dest_root, filename_template.format(get_fname_ext(imf)[0])), img) print("Image", imf, "shift done !")
def batch_matlab_resize_images(ori_root, dest_root, scale=1.0, method='bicubic', filename_template="{}.png"): ''' function: resizing images in batches, same as matlab2017 imresize params: ori_root: string, the dir of images that need to be processed dest_root: string, the dir to save processed images scale: float, the resize scale method: string, the interpolation method, optional: 'bilinear', 'bicubic' default: 'bicubic' filename_template: string, the filename template for saving images ''' if method != 'bilinear' and method != 'bicubic': raise Exception('Unknown method!') handle_dir(dest_root) scale = float(scale) images_fname = sorted(listdir(ori_root)) for imf in images_fname: img = cv2.imread(os.path.join(ori_root, imf)).astype('float32') img = matlab_imresize(img, scalar_scale=scale, method=method) cv2.imwrite( os.path.join(dest_root, filename_template.format(get_fname_ext(imf)[0])), img) print("Image", imf, "resize done !")
def add_files_postfix(root, postfix=''): ''' add postfix to filename params: root: the dir of files that need to be processed postfix: the postfix to be added ''' img_list = glob_match(os.path.join(root, "*")) for im in img_list: fname, ext = get_fname_ext(im) dest_basename = "{}{}.{}".format(fname, postfix, ext) src = im dst = os.path.join(root, dest_basename) rename_file(src, dst)
def resort_files_index(root, template='{:0>4}', start_idx=0): ''' resort files' filename using template that index start from start_idx params: root: the dir of files that need to be processed template: the template for processed filename start_idx: the start index ''' template = template + '.{}' img_list = sorted(glob_match(os.path.join(root, "*"))) for i, im in enumerate(img_list): fname, ext = get_fname_ext(im) dest_basename = template.format(i + start_idx, ext) src = im dst = os.path.join(root, dest_basename) rename_file(src, dst)
def remove_files_postfix(root, postfix=''): ''' remove postfix from filename params: root: the dir of files that need to be processed postfix: the postfix to be removed ''' img_list = glob_match(os.path.join(root, "*")) for im in img_list: fname, ext = get_fname_ext(im) now_postfix = fname[-len(postfix):] if now_postfix == postfix: dest_basename = "{}.{}".format(fname[:-len(postfix)], ext) src = im dst = os.path.join(root, dest_basename) rename_file(src, dst)
def save_flow_pt2mat(ori_root, dest_root): ''' 把 ori_root 中视频的 flow 从 pt 文件转换为 mat 文件后保存到 dest_root 保存在 pt 文件中的 flow 的 shape=[1, 2, h, w] ''' handle_dir(dest_root) video_list = listdir(ori_root) for v in video_list: handle_dir(os.path.join(dest_root, v)) file_list = glob_match(os.path.join(ori_root, v, "*.pt")) for pt_path in file_list: fname, ext = get_fname_ext(pt_path) mat_path = os.path.join(dest_root, v, "{}.mat".format(fname)) try: flow = torch.load(pt_path)[0].permute(1, 2, 0).cpu().numpy() flow_dict = {'flow': flow} scio.savemat(mat_path, flow_dict) print('save {} to {}'.format(pt_path, mat_path)) except: print('skip file {}'.format(pt_path))
def batch_cv2_resize_images(ori_root, dest_root, scale=1.0, method='bicubic', filename_template="{}.png"): ''' function: resizing images in batches params: ori_root: string, the dir of images that need to be processed dest_root: string, the dir to save processed images scale: float, the resize scale method: string, the interpolation method, optional: 'nearest', 'bilinear', 'bicubic' default: 'bicubic' filename_template: string, the filename template for saving images ''' if method == 'nearest': interpolation = cv2.INTER_NEAREST elif method == 'bilinear': interpolation = cv2.INTER_LINEAR elif method == 'bicubic': interpolation = cv2.INTER_CUBIC else: raise Exception('Unknown method!') handle_dir(dest_root) scale = float(scale) images_fname = sorted(listdir(ori_root)) for imf in images_fname: img = cv2.imread(os.path.join(ori_root, imf)).astype('float32') img = cv2.resize(img, dsize=(0, 0), fx=scale, fy=scale, interpolation=interpolation) cv2.imwrite( os.path.join(dest_root, filename_template.format(get_fname_ext(imf)[0])), img) print("Image", imf, "resize done !")