系列文章目录
第一章PIL单通道图像处理
前言
将单通道图像以彩色图像的形式进行保存主要使用了PIL库
一、代码实现
palette_data = [***]:可以进行自定义设置
代码如下:
from PIL import Image
label = np.asarray(Image.Open(r"*.tif"),np.uint8)
tmp_image = Image.fromarray(tmp_image, "P")
palette_data = [0, 0, 0, 255, 255, 255, 255, 69, 0, 0, 255, 127] # 调色板.三个数字唯一组RGB色彩表示
tmp_image.putpalette(palette_data)
tmp_image.save(r"*.png")
使用默认调色板
lbl_pil = Image.fromarray(mask.astype(np.uint8), mode="P")
colormap = imgviz.label_colormap()
lbl_pil.putpalette(colormap.flatten())
lbl_pil.save(save_path)
批处理
# from PIL import Image
# label = np.asarray(Image.Open(r"*.tif"),np.uint8)
# tmp_image = Image.fromarray(tmp_image, "P")
# palette_data = [0, 0, 0, 255, 255, 255, 255, 69, 0, 0, 255, 127] # 调色板.三个数字唯一组RGB色彩表示
# tmp_image.putpalette(palette_data)
# tmp_image.save(r"*.png")
import os
import glob
from PIL import Image
import numpy as np
# src与targ文件夹
src = r""
targ = r""
if not os.path.exists(targ):
os.mkdir(targ)
# 获取文件加下的所有png文件
png_files = glob.glob(os.path.join(src, "*.png"))
# 循环遍历文件
for png_file in png_files:
label = np.asarray(Image.open(png_file),np.uint8)
tmp_image = Image.fromarray(label, "P")
palette_data = [0, 0, 0, 255, 0, 0,</