モブ沢工房

プログラミングとかLinux関連(特にOSSのグラフィックツール関連)とかレトロゲームとか3Dプリンタやら日曜大工等、色々。

GIMPでG'MICのColorize(comic)向けレイヤ分割スクリプト

最近になってG'MICプラグインのColorize(comic)を知ったのですが、本当に凄いですね。

これで下塗りが楽になったので、ものすごい速度向上しました。

ちなみにワタクシのお絵かきブログにて備忘録的にメモしていたりして…で、そこで少しだけ愚痴っているように、レイヤを色ごとに分けるオプションでやるとものすごい数のレイヤを作ってきて大変なことになるので、主線+色1枚のレイヤで出力させているのです。

それで、いままで色範囲選択->cut->paste->レイヤ化でちまちまとレイヤを分けていたのです。

でもやっぱり面倒くさい。それで作りました。自動レイヤ分けスクリプトです。

仕組みはごく簡単、色レイヤをアクティブにしてこのスクリプトを起動すると、自動的に使われている色を検出してレイヤ分けします。

ここで注意せねばならないのは、あまりにも色領域が小さいと無視されることです。 なぜなら、ピクセルのサーチには64x64にリサイズした複製レイヤを使っているからです(^^;

っと、ここまで書いて気づいたのですがdothikoutil.pyという自作ライブラリを使っておりますな…そんなわけで、少し書きなおしましたが、本来はメンテナンスの為にまとめています。そのうちgithubに公開リポジトリを作って、そこに今まで作ったようなgimpスクリプトをまとめて置いておこうと思いますです…

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#[license] GPLv3
#[plugin]
#[name] layer-divide-by-color
#[desc] 
#レイヤを色で分割するスクリプト。
#G'MICのColorize(comic)で出力された彩色レイヤの分割向け。
#[version]
#0.1 初期リリース
#[end]

#  このプログラムはGPLライセンスver3で公開します。
# 
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You may have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.           
#
#   Copyright (C) 2016 dothiko(http://dothiko.hatenablog.com/) 


from gimpfu import *
#from dothikoutil import *

def scale_layer(layer,w,h,method,scale_itself=False):
    """
    method is 
    INTERPOLATION-NONE (0), 
    INTERPOLATION-LINEAR (1), 
    INTERPOLATION-CUBIC (2), 
    INTERPOLATION-LANCZOS (3) 
    """
    if not scale_itself:
        layer=duplicate_layer(layer)

    old_method=pdb.gimp_context_get_interpolation()
    pdb.gimp_context_set_interpolation(method)
    pdb.gimp_layer_scale(layer,w,h,1)
    pdb.gimp_context_set_interpolation(old_method)
    return layer

def duplicate_layer(layer,pos=0):
    """
    Utility function,to automatically insert copied layer to its image.
    """
    nl=pdb.gimp_layer_copy(layer,0)
    pdb.gimp_image_insert_layer(layer.image,nl,None,pos)
    return nl


def python_fu_layer_divide(a_img,a_drawable,sample_arg=True):

  
    pdb.gimp_context_push()
    pdb.gimp_image_undo_group_start(a_img)

    try:
        pdb.gimp_context_set_sample_threshold(0.0)
        w=64
        h=64
        pixels={}
        sl=scale_layer(a_drawable,w,h,0)# 0 means "no-interporation"
        iy=0
        while iy < h:
            ix=0
            while ix < w:
                p=sl.get_pixel(ix,iy)
                if p == (0,0,0,0):
                    pass
                elif p in pixels:
                    pixels[p]+=1
                else:
                    pixels[p]=1
                ix+=1
            iy+=1

        if pdb.gimp_selection_is_empty(a_img)==0:
            save=pdb.gimp_selection_save(a_img)
        else:
            save=None
        
        # Enumerate pixel and divide layer by pixel color.
        # The last color component is remained at original layer,
        # because there should be a empty layer after all colors floated.
        for cc in pixels.keys()[:-1]:
            pdb.gimp_image_select_color(a_img,2,a_drawable,cc)
            fl=pdb.gimp_selection_float(a_drawable,0,0)
            pdb.gimp_floating_sel_to_layer(fl)

        if save:
            pdb.gimp_selection_load(save)
        else:
            pdb.gimp_selection_none(a_img)

        a_img.remove_layer(sl)

    finally:
        pdb.gimp_image_undo_group_end(a_img)
        pdb.gimp_context_pop()



register(
        "python_fu_layer_divide",
        "layer-divide-by-color",
        "divide a layer from its color",
        "dothiko",
        "kakukaku world",
        "dec 2015", 
        "<Image>/Python-Fu/layer/layer-divide-by-color", 
        "RGB*,GRAY*",
        [
          # (PF_BOOL,"sample_arg","sample of argument",True),
        ],
        [],
        python_fu_layer_divide)


main()