Python 索引色规范化

由于ps保存windows索引色 标注物的索引值不是想要的背景为0 标注物为1,所以需要对png索引图在规范化处理。或者在保存png时自定义索引

主要使用PYTHON实现,但第一版由于单线程处理太慢。100张500*500 逐像素要43秒。

import os
import shutil
from PIL import Image
import datetime


def TM_png2ind(raw_img_path, save_img_path, label_value):
    now_file_path = raw_img_path
    for dirpath, dirnames, filenames in os.walk(now_file_path):
        for filename in filenames:
            file_path = now_file_path + filename
            img = Image.open(file_path).convert('P')
            # 暴力像素扫描
            for x in range(img.size[0]):
                for y in range(img.size[1]):
                    if img.getpixel((x, y)) == int(label_value):  # 给标注部分分配一个像素值,多分类分配多个像素值
                        img.putpixel((x, y), 1)
                    else:
                        img.putpixel((x, y), 0)  # 背景部分

                img.putpalette([0, 0, 255, 255, 0, 0])  # 一分类, 【0,0,0】是背景,【0,255,0】是标注物
                save_file_path = save_img_path + filename
                img.save(save_file_path)


if __name__ == '__main__':
    start_t = datetime.datetime.now()

    str = []
    raw_img_path = "C:\\Users\\wujq\\Desktop\\png\\png\\"
    save_img_path = "C:\\Users\\wujq\\Desktop\\png\\png1\\"
    label_value = 249 # 目前index值

    if (os.path.exists(save_img_path)):
        shutil.rmtree(save_img_path)
    os.mkdir(save_img_path)
    TM_png2ind(raw_img_path, save_img_path, label_value)

    end_t = datetime.datetime.now()
    elapsed_sec = (end_t - start_t).total_seconds()
    print("消耗: " + "{:.2f}".format(elapsed_sec) + " 秒")

第二版采用多线程处理,128张500*500要18秒,提升很大。

import os
import shutil
from PIL import Image
import datetime
import multiprocessing as mp

def TM_png2ind(file_path):

    save_img_path = "C:\\Users\\wujq\\Desktop\\png\\png1\\"  # 输出目录----------
    label_value = 249  # 目前index值---------------------------------------------
    filename = file_path.split("\\")[6]   # 切分提取文件名

    # 读取图片
    img = Image.open(file_path).convert('P')
    # 暴力像素扫描
    for x in range(img.size[0]):
        for y in range(img.size[1]):
            if img.getpixel((x, y)) == int(label_value):  # 给标注部分分配一个像素值,多分类分配多个像素值
                img.putpixel((x, y), 1)
            else:
                img.putpixel((x, y), 0)  # 背景部分

    img.putpalette([0, 0, 255, 255, 0, 0])  # 一分类, 【0,0,255】是背景,【255,0,0】是标注物
    save_file_path = save_img_path + filename
    img.save(save_file_path)


if __name__ == '__main__':
    start_t = datetime.datetime.now()
    str = []
    raw_img_path = "C:\\Users\\wujq\\Desktop\\png\\png\\"
    save_img_path = "C:\\Users\\wujq\\Desktop\\png\\png1\\"

    if (os.path.exists(save_img_path)):
        shutil.rmtree(save_img_path)
    os.mkdir(save_img_path)

    now_file_path = raw_img_path
    for dirpath, dirnames, filenames in os.walk(now_file_path):
        for filename in filenames:
            file_path = now_file_path + filename
            str.append(file_path)

    pool = mp.Pool(6)
    rel = pool.map(TM_png2ind, str)


    end_t = datetime.datetime.now()
    elapsed_sec = (end_t - start_t).total_seconds()
    print("消耗: " + "{:.2f}".format(elapsed_sec) + " 秒")

你说我一个实施,硬生生弄得啥都知道点了。唉。。。太难了