16 bit to 8 bit

import os
import subprocess

def check_ext_and_filename(file_path : str) -> str:
    '''
    file path에서 ext, file name 분리
    '''
    ext = os.path.splitext(file_path)[-1].lower()
    file_name = os.path.splitext(file_path)[0]
    return ext, file_name

def scale_16bit_to_8bit(file_path : str, dst_path : str) -> str:
    '''
    16bit tiff를 8bit tiff로 변환
    '''
    filename = os.path.basename(file_path)
    dst_file_path = os.path.join(dst_path, filename)
    cmd = f'gdal_translate -ot Byte -scale 0 65535 0 255 {file_path} {dst_file_path}'
    subprocess.call(cmd, shell=True)
    return filename

scene_dir = '/nas/Dataset/COD/PSS/area2_tiff'
dst_dir = '/nas/Dataset/COD/PSS/8-bit'
scene_list = []

for (root, dirs, files) in os.walk(scene_dir):
    for file_name in files:
        file_path = os.path.join(root, file_name)
        ext, filename = check_ext_and_filename(file_path)
        
        if ext == '.tif' or ext == '.jp2':
            scene_list.append(file_path)
            
for i in scene_list:
    print(scale_16bit_to_8bit(i, dst_dir))

dst_dir = '/nas/Dataset/COD/PSS/8-bit'
file_path = '/nas/Dataset/COD/PSS/area1_tiff/Planet_20211024_area1.tif'
print(scale_16bit_to_8bit(file_path, dst_dir))