使用Python将16bit转8bit

代码

import glob

import cv2
import numpy as np
def transfer16to8(file):
    image_16bit = cv2.imread(file, cv2.IMREAD_UNCHANGED)
    min_16bit = np.min(image_16bit)
    max_16bit = np.max(image_16bit)
    image_8bit = np.array(np.rint(255 * ((image_16bit - min_16bit) / (max_16bit - min_16bit))), dtype=np.uint8)
    cv2.imwrite(file, image_8bit)
files = glob.glob("data/*.tif")
for file in files:
    transfer16to8(file)