8位转24位
import numpy as np
from PIL import Image
import os
path='out4/'# 图片原始存放位置
newpath='out5/' # 修改后图片存放位置
def turnto24(path):
files = os.listdir(path)
files = np.sort(files)
for f in files:
imgpath = path + f
img=Image.open(imgpath).convert('RGB')
dirpath = newpath
file_name, file_extend = os.path.splitext(f)
dst = os.path.join(os.path.abspath(dirpath), file_name + '.jpg')
img.save(dst)
turnto24(path)
24位转8位
import os
import numpy as np
from PIL import Image
path = r'out5\\'
save_path = r'out4\\'
for i in os.listdir(path):
img = Image.open(path+i)
img = Image.fromarray(np.uint8(img))
t = img.convert('L')
img = Image.fromarray(np.uint8(t)) # *255
img.save(save_path+i)