32位转换8位图像

from PIL import Image
import numpy as np
import math
import os

path = 'C:\\Users\\Administrator\\Desktop\\old_label\\'
newpath = 'C:\\Users\\Administrator\\Desktop\\mask\\'


def toeight():
    filelist = os.listdir(path)  # 该文件夹下所有的文件(包括文件夹)
    for file in filelist:
        whole_path = os.path.join(path, file)
        img = Image.open(whole_path)  # 打开图片img = Image.open(dir)#打开图片
        img = np.array(img)
        # img = Image.fromarray(np.uint8(img / float(math.pow(2, 16) - 1) * 255))
        img = Image.fromarray(np.uint8(img))
        img.save(newpath + file)


toeight()

有点问题 32位图像转换出来是24位的,但是代码能用

 

import os
from scipy import ndimage
import pickle as plt
import cv2 as cv
from PIL import Image
import numpy as np
import math
import os

ii = cv.imread("0.bmp")
gray =  cv.cvtColor(ii, cv.COLOR_BGR2GRAY)
print(gray)
cv.imshow("gray", gray)
cv.imwrite('1.jpg',gray)

这一段可以转换~~~

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页