File size: 1,627 Bytes
0f90f73 |
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 58 59 60 61 62 63 64 |
import PIL.Image as Image
import numpy as np
import os
def main(args):
if not args.indir.endswith('/'):
args.indir += '/'
os.makedirs(args.outdir, exist_ok=True)
src_images = [
args.indir+fname for fname in os.listdir(args.indir)]
tgt_masks = [
args.outdir+fname[:-4] + f'_mask000.png'
for fname in os.listdir(args.indir)]
for img_name, msk_name in zip(src_images, tgt_masks):
#print(img)
#print(msk)
image = Image.open(img_name).convert('RGB')
image = np.transpose(np.array(image), (2, 0, 1))
mask = (image == 255).astype(int)
print(mask.dtype, mask.shape)
Image.fromarray(
np.clip(mask[0,:,:] * 255, 0, 255).astype('uint8'),mode='L'
).save(msk_name)
'''
for infile in src_images:
try:
file_relpath = infile[len(indir):]
img_outpath = os.path.join(outdir, file_relpath)
os.makedirs(os.path.dirname(img_outpath), exist_ok=True)
image = Image.open(infile).convert('RGB')
mask =
Image.fromarray(
np.clip(
cur_mask * 255, 0, 255).astype('uint8'),
mode='L'
).save(cur_basename + f'_mask{i:03d}.png')
'''
if __name__ == '__main__':
import argparse
aparser = argparse.ArgumentParser()
aparser.add_argument('--indir', type=str, help='Path to folder with images')
aparser.add_argument('--outdir', type=str, help='Path to folder to store aligned images and masks to')
main(aparser.parse_args())
|