Created
October 18, 2019 19:04
-
-
Save asanakoy/43cc2fa4b582c6da80adee05bcceef00 to your computer and use it in GitHub Desktop.
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
import cv2 | |
from pathlib import Path | |
import os | |
import re | |
import pandas as pd | |
import numpy as np | |
from tqdm import tqdm | |
def main(input_dirs, output_dir): | |
output_dir = Path(output_dir) | |
output_dir.mkdir(parents=True, exist_ok=True) | |
files = [] | |
df = pd.DataFrame() | |
column_names = list(map(lambda x: x.replace('/', '_'), input_dirs)) | |
for d, column_name in zip(input_dirs, column_names): | |
paths = os.listdir(d) | |
print(paths[:3]) | |
paths = list(filter(lambda x: re.match(".*?\.((jpg)|(jpeg)|(png))", x) is not None, paths)) | |
paths.sort() | |
paths = list(map(lambda x: os.path.join(d, x), paths)) | |
names = list(map(lambda x: Path(x).stem, paths)) | |
print(d) | |
print('---') | |
print(paths) | |
cur_df = pd.DataFrame(index=names, data={column_name: paths}) | |
df = df.join(cur_df, how='outer') | |
# order columns | |
df = df[column_names] | |
print('===') | |
print('===') | |
print(df.head(3)) | |
print(df.columns) | |
for row in tqdm(df.itertuples()): | |
dest_path = output_dir / (row.Index + '.jpg') | |
images = [] | |
for i, name in enumerate(column_names): | |
image_path = row[i + 1] | |
image = cv2.imread(image_path) | |
assert image is not None, image_path | |
images.append(image) | |
# assume that the images are all of the same size | |
concat_image = np.hstack(images) | |
#print(concat_image.shape) | |
cv2.imwrite(str(dest_path), concat_image) | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('dirs', nargs='+') | |
parser.add_argument('-o', '--output_dir', required=True) | |
args = parser.parse_args() | |
print(args) | |
main(args.dirs, args.output_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment