「psd2waifu2x」ソースコード

これらのソースコードは改変などは基本的に自由に行ってもらって構いません。
ただ、改善や機能追加等をして頂けた場合は、僕自身興味がありますのでコメント欄などにご一報いただけると幸いです。


psd2wife.py


#coding:utf-8

from psd_tools import PSDImage
import os, copy, subprocess, datetime, glob, sys
from configparser import ConfigParser

class PSDPaser:
    def __init__(self, dir_psd, dir_cfg = os.path.join(os.path.dirname(__file__), "psd2wife.ini"), safe_mode = True):
        self.config = ConfigParser()

        self.config.read(dir_cfg, "utf-8")
        self.wf_scale = float(self.config.get("waifu", "wife_scale"))
        self.dir_safe = self.config.get("save", "safety")

                
        self.dir_psd = dir_psd
        self.psd = PSDImage.open(dir_psd)

        self.psd_all = [i for i in self.psd.descendants()]
        
        if safe_mode:
            self.dir_save_base  = os.path.join(self.dir_safe, datetime.datetime.today().strftime("%Y%m%d_%H%M_%S"))
        else:
            self.dir_save_base = self.dir_psd.replace(".psd", "")
        self.dir_save_pics  = os.path.join(self.dir_save_base, "result") 
        
        list_file_name = []
        
        structure = []
        structure_all = []
        structure_hrchy = []
        
        structure_safe = []
        structure_safe_all = []
        structure_safe_hrchy = []
        psd_setting=[[str(int(self.psd.width * self.wf_scale)), str(int(self.psd.height * self.wf_scale))]]
        
        print("「{}」\nに.psdの各レイヤーを画像に変換します。".format(self.dir_save_pics))
        for psd_elem_num, psd_elem in enumerate(self.psd_all):
            if str(psd_elem.parent.name) == "Root":
                structure = []
                
            loop=0
            structure = []
            structure_safe = []
            
            psd_group = copy.deepcopy(psd_elem.parent)
            while psd_group.is_group():
                loop+=1
                structure.append(psd_group.name)
                if str(psd_group.name) != "Root":
                    psd_group = psd_group.parent
                else:
                    break
            try:
                structure.remove("Root")
            except:
                pass
                
            file_name = psd_elem.name
            list_file_name.append(file_name)
            
            if safe_mode:
                for i in structure:
                    if structure_all.count(i) == 0:
                        structure_all.append(i)
                    structure_safe.append("Group_{}".format(str(structure_all.index(i)+1)))
                for i in structure_safe:
                    if structure_safe_all.count(i) == 0:
                        structure_safe_all.append(i)
            
            if structure_hrchy.count(structure) == 0 and structure != []:
                structure_hrchy.append(structure)
                
                structure_safe_hrchy.append(structure_safe)
                
            if safe_mode:                
                structure = structure_safe
                file_name = "layer_{}".format(str(psd_elem_num+1))
                
            os.makedirs(os.path.join(self.dir_save_pics, *structure[::-1]), exist_ok=True)
            dir_save = os.path.join(self.dir_save_pics, *structure[::-1], file_name + ".png")
            psd_setting.append([dir_save, str(int(psd_elem.offset[0] * self.wf_scale)), str(int(psd_elem.offset[1] * self.wf_scale)), "_".join([*structure[::-1], file_name]), str(int(psd_elem.is_visible())), structure[0] if structure != [] else "this_is_base_group"])
            if not psd_elem.is_group():
                try:
                    layer = psd_elem.topil()
                    layer.save(dir_save)
                except:
                    print("{}は保存されませんでした。".format(psd_elem.name))
                    
        self.gimp_structure_all = "[\"{}\"]".format("\",\"".join(structure_all))
        self.gimp_structure_hrchy = "[{}]".format(",".join(["[\"{}\"]".format("\",\"".join(i)) for i in structure_hrchy]))
        self.gimp_structure_safe_all = "[\"{}\"]".format("\",\"".join(structure_safe_all))
        self.gimp_structure_safe_hrchy = "[{}]".format(",".join(["[\"{}\"]".format("\",\"".join(i)) for i in structure_safe_hrchy]))
        
        self.gimp_file_name = "[\"{}\"]".format("\",\"".join(list_file_name[::-1]))
                
        print("変換終了")
        str_ = "\n".join([",".join(i) for i in psd_setting])
        self.dir_save_setting = self.dir_save_pics+".txt"
        with open(self.dir_save_setting, "wt") as f:
            f.write(str_)

        
    def waifu_run(self):
        dir_waifu = self.config.get("waifu", "directory")
        try:
            cmd="\"{}\" -i \"{}\" -s {} {}".format(dir_waifu, self.dir_save_pics, str(self.wf_scale),self.config.get("waifu", "setting"))
        except:
            cmd="\"{}\" -i \"{}\" -s {}".format(dir_waifu, self.dir_save_pics, str(self.wf_scale))
        print("「{}」を実行します。しばらく時間が借るので放置してください。".format(cmd))
        subprocess.call(cmd)
        
    def gimpImportCommand(self):
        dir_converted = glob.glob(self.dir_save_pics+"*"+ os.sep)
        os.rename(self.dir_save_pics, self.dir_save_pics+"_preWaifu2x")
        os.rename(dir_converted[1], dir_converted[0])
        
        dir_save_gimp_cmd = os.path.join(self.dir_save_base, "gimp_cmd.txt") 
        dir_save_gimp = self.dir_save_setting.replace(".txt", ".xcf")
        gimp_cmd='''import codecs
dir_txt = r"{}"
dir_save = r"{}"

structure_all = {}
structure_hrchy = {}
structure_safe_all = {}
structure_safe_hrchy = {}

gimp_file_name = {}

with codecs.open(dir_txt, 'r', 'utf-8', 'ignore') as f:
    im_setting = f.read()

list_ims = [i.split(",") for i in im_setting.split("\\n")]
image = gimp.Image(int(list_ims[0][0]), int(list_ims[0][1]), RGB)

list_group=[pdb.gimp_layer_group_new(image) for i in structure_safe_all]

for i in range(len(list_group)):
    #list_group[i].name = structure_safe_all[i]
    list_group[i].name = structure_all[i]

base_group = []
list_hierarchy = []
for i in range(len(structure_safe_hrchy)):
        if len(structure_safe_hrchy[i])>1:
            list_hierarchy.append(structure_safe_hrchy[i])
        else:
            base_group.append(structure_safe_hrchy[i])

base_group = base_group[::-1]
list_hierarchy = list_hierarchy[::-1]
for  i in range(len(base_group)):
    image.add_layer(list_group[structure_safe_all.index(base_group[i][0])], i)


list_remove = []
for i in range(len(list_hierarchy)):
    test = list_hierarchy[i][::-1]
    if len(test)>1:
        for j in range(len(test)-1):
            check=[test[j+1], test[j]]
            if not check in list_remove:
                list_remove.append(check)
                group_check = [list_group[structure_safe_all.index(check[0])], list_group[structure_safe_all.index(check[1])]]
                pdb.gimp_image_insert_layer(image, group_check[0], group_check[1], i)
                print(check)

gimp.Display(image)

for num_setting in range(len(list_ims[1:][::-1])):
    layer_setting = list_ims[1:][::-1][num_setting]
    try:
        layer = pdb.gimp_file_load_layer(image, layer_setting[0])
        layer.set_offsets(int(layer_setting[1]), int(layer_setting[2]))
        layer.name = gimp_file_name[num_setting]
        pdb.gimp_drawable_set_visible(layer, int(layer_setting[4]))
        
        key = layer_setting[5].replace("\\r","")
        gimp.message(key)
        if key == "this_is_base_group":
            image.add_layer(layer, num_setting)
        else:
            pdb.gimp_image_insert_layer(image, layer, list_group[structure_safe_all.index(key)], num_setting)

    except:
        pass

drawable = pdb.gimp_image_get_active_layer(image)
pdb.gimp_xcf_save(0, image, drawable, dir_save, dir_save)

gimp.Display(image)

'''.format(self.dir_save_setting, dir_save_gimp, self.gimp_structure_all, self.gimp_structure_hrchy, self.gimp_structure_safe_all, self.gimp_structure_safe_hrchy, self.gimp_file_name)


        with open(dir_save_gimp_cmd,"w",encoding="utf-8") as gimp_file:
            gimp_file.write(gimp_cmd)
        subprocess.run("explorer {}".format(self.dir_save_base))



if __name__=="__main__":
    print("グループ名が重複しているとバグが発生して正常に変換できません。\n手動でPSDファイルを編集して、グループ名が重複しないようにリネームしてください。\n(例: 「Group」→「Group #1」)")
    print("もしくは1度Gimpでpsdファイルを開き、再エクスポートしてください。\n重複が自動で解決されます。")
    dir_psd = input("waifu2xで変換したい.psdファイルをD&DしてEnterを押してね!")
    psdp = PSDPaser(dir_psd)
    psdp.waifu_run()
    psdp.gimpImportCommand()

psd2wife.ini


[save]
safety=G:\Dev_Doc\gimp
[waifu]
directory   = O:\files\waifu2x-caffe\waifu2x-caffe-cui.exe
wife_scale  = 2.0

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です