A simple image concat script
โ Posts & Notes ยท
21/05/2021
ยท
1 minute
Usage
./image_concat.py --resize 256x256 hazelnut.png leather.png out.png
Result
.](https://user-images.githubusercontent.com/9824244/119120482-9cfbb100-ba2c-11eb-98eb-a66665406deb.png)
Source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!python | |
| import argparse | |
| from PIL import Image | |
| from torchvision import utils, transforms | |
| def parse_args(): | |
| parser = argparse.ArgumentParser("Image concat") | |
| parser.add_argument("inputs", metavar="input", nargs="+") | |
| parser.add_argument("output") | |
| parser.add_argument("--resize", type=str) | |
| return parser.parse_args() | |
| def main(args): | |
| inputs = args.inputs | |
| output = args.output | |
| size = args.resize | |
| if size is not None: | |
| to_tensor = transforms.Compose([ | |
| transforms.ToTensor(), | |
| transforms.Resize(tuple(map(int, size.split("x")))), | |
| ]) | |
| else: | |
| to_tensor = transforms.ToTensor() | |
| img_tensors = [] | |
| for input_file in inputs: | |
| img = Image.open(input_file) | |
| img = to_tensor(img) | |
| img_tensors.append(img) | |
| utils.save_image(img_tensors, output, ncol=1) | |
| if __name__ == "__main__": | |
| args = parse_args() | |
| main(args) |