|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import torch |
| 4 | +import random |
| 5 | +import argparse |
| 6 | +from PIL import Image, ImageFilter, ImageOps |
| 7 | +from multiprocessing import Pool, cpu_count |
| 8 | +from timm.data.transforms import RandomResizedCropAndInterpolation |
| 9 | +import torchvision.transforms as transforms |
| 10 | + |
| 11 | +Image.MAX_IMAGE_PIXELS = 6400000000 |
| 12 | + |
| 13 | + |
| 14 | +def build_transform(input_size): |
| 15 | + train_interpolation = "bicubic" |
| 16 | + t = [ |
| 17 | + RandomResizedCropAndInterpolation(input_size, scale=(0.5, 1.0), interpolation=train_interpolation), |
| 18 | + transforms.RandomHorizontalFlip(), |
| 19 | + ] |
| 20 | + t = transforms.Compose(t) |
| 21 | + |
| 22 | + return t |
| 23 | + |
| 24 | + |
| 25 | +def pil_loader(path): |
| 26 | + with open(path, "rb") as f: |
| 27 | + img = Image.open(f) |
| 28 | + return img.convert("RGB") |
| 29 | + |
| 30 | + |
| 31 | +def save_image(transformed_img, output_image_path): |
| 32 | + if isinstance(transformed_img, torch.Tensor): |
| 33 | + transformed_img = transforms.ToPILImage()(transformed_img) |
| 34 | + transformed_img.save(output_image_path) |
| 35 | + |
| 36 | + |
| 37 | +def get_image_files(input_dir): |
| 38 | + for root, _, files in os.walk(input_dir): |
| 39 | + for file in files: |
| 40 | + if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')): |
| 41 | + yield os.path.join(root, file) |
| 42 | + |
| 43 | + |
| 44 | +def transform_and_save_crops(args): |
| 45 | + input_path, input_dir, output_dir, transform = args |
| 46 | + print(input_path) |
| 47 | + file_basename = os.path.basename(input_path) |
| 48 | + |
| 49 | + img = pil_loader(input_path) |
| 50 | + transformed_img = transform(img) |
| 51 | + output_image_path = os.path.join(output_dir, file_basename) |
| 52 | + save_image(transformed_img, output_image_path) |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == '__main__': |
| 56 | + parser = argparse.ArgumentParser(description='Save transformed images in a directory.') |
| 57 | + parser.add_argument('input_dir', help='Path to the input directory.') |
| 58 | + parser.add_argument('output_dir', help='Path to the output directory.') |
| 59 | + parser.add_argument('-p', '--processes', type=int, default=cpu_count(), help='Number of processes to use. Default: number of CPU cores') |
| 60 | + parser.add_argument('--input_size', type=int, default=16384, help='input image size') |
| 61 | + args = parser.parse_args() |
| 62 | + |
| 63 | + input_dir = args.input_dir |
| 64 | + output_dir = args.output_dir |
| 65 | + num_processes = args.processes |
| 66 | + input_size = args.input_size |
| 67 | + print("num_processes: {}".format(num_processes)) |
| 68 | + print("input_size: {}".format(input_size)) |
| 69 | + |
| 70 | + transform = build_transform(input_size=input_size) |
| 71 | + |
| 72 | + image_files = list(get_image_files(input_dir)) |
| 73 | + task_args = [(file, input_dir, output_dir, transform) for file in image_files] |
| 74 | + |
| 75 | + os.makedirs(output_dir, exist_ok=True) |
| 76 | + |
| 77 | + with Pool(processes=num_processes) as pool: |
| 78 | + pool.map(transform_and_save_crops, task_args) |
0 commit comments