chundoong-lab-ta/SamsungDS22/project/project_skeleton/common/img2bin

35 lines
1.0 KiB
Plaintext
Raw Normal View History

2022-09-29 18:01:45 +09:00
#!/usr/bin/env python3
import argparse
import struct
import numpy as np
from PIL import Image
from skimage import color
def run(args):
total_bytes = 0
with open(args.dst, 'wb') as f:
for img_path in args.src:
print(f'Processing {img_path}...')
img = np.asarray(Image.open(img_path))
if img.ndim == 2:
img = np.tile(img[:,:,None], 3)
img = img[:,:,:3]
img = np.asarray(Image.fromarray(img).resize((256, 256), resample=3))
img = color.rgb2lab(img)[:,:,0]
img = img.flatten()
data = struct.pack(f'{img.size}f', *img)
f.write(data)
total_bytes += len(data)
print(f'{total_bytes} bytes written to {args.dst}')
def main():
parser = argparse.ArgumentParser()
parser.add_argument('src', nargs='+', help='Input images. (e.g., imgs/*)')
parser.add_argument('dst', help='Output binary name. (e.g., input.bin)')
args = parser.parse_args()
run(args)
if __name__ == '__main__':
main()