0

うまく機能する Python Gimp スクリプトがありますが、問題が 1 つあります。

画像に複数の線を配置する必要がある場合、2 つの別々のテキスト レイヤーを作成する必要があります。

             #Sample of text parameter xml file
        <txtLyrs>
        <extra_txtLyr>
        <extra_txtLyr txtLyrName="lblLayer6" LyrFontColor="WHITE" \
        LyrFontName="Interstate-Bold Bold" LyrFontSize="15.0" txtLyrString=\
        "Some Text" txtX="460" txtY="331" txtlyr_height="17" txtlyr_width="73" index="71" />
        <extra_txtLyr txtLyrName="lblLayer5" LyrFontColor="WHITE" \
        LyrFontName="Interstate-Bold Bold" LyrFontSize="8.0" txtLyrString=\
        "Some Text [10][13] Really Long Text" txtX="676" txtY="144" txtlyr_height="9"\
         txtlyr_width="95" index="70" />
        <extra_txtLyr txtLyrName="lblLayer4" LyrFontColor="WHITE" \
        LyrFontName="Interstate-Bold Bold" LyrFontSize="8.0" txtLyrString=\
        "Some Text" txtX="676" txtY="130" txtlyr_height="9" txtlyr_width="125" index="69" />
        <extra_txtLyr txtLyrName="lblLayer3" LyrFontColor="WHITE" \
        LyrFontName="Interstate-Bold Bold" LyrFontSize="15.0" txtLyrString=\
        "Some Text" txtX="678" txtY="331" txtlyr_height="17" txtlyr_width="72" index="68" />
        </extra_txtLyrs>
        </txtLyrs>

        #Constants removed to make smaller block

        #Helper function to read parameters from xml file
        def get_extratxt_btn_params( active_name ): 

            active_dgm = active_name
            extraTxtLayers = xmldoc.getElementsByTagName("txtLyrs")[0]

            if active_dgm:
                siteTxtLayers = xmldoc.getElementsByTagName("extra_txtLyr")[0]
                extraButtonTxtLayers = siteTxtLayers.getElementsByTagName("extra_txtLyr")
            else:
                siteTxtLayers = xmldoc.getElementsByTagName("other_txtLyr")[0]
                extraButtonTxtLayers = siteTxtLayers.getElementsByTagName("other_txtLyr")

            extraTxtParms = {}
            for lyrParm in extraButtonTxtLayers:
                lyrParams = [ lyrParm.getAttribute("txtLyrName"), \
                lyrParm.getAttribute("LyrFontName"), \
                lyrParm.getAttribute("LyrFontSize"), lyrParm.getAttribute("txtLyrString"), \
                lyrParm.getAttribute("LyrFontColor"), lyrParm.getAttribute("txtlyr_height"), \
                lyrParm.getAttribute("txtlyr_width"), lyrParm.getAttribute("txtX"), \
                lyrParm.getAttribute("txtY")]
                extraTxtParms [lyrParm.getAttribute("index")] = lyrParams
            return extraTxtParms

         ##**function called by GIMP to create text layers from xml file

        def dgm_extratxtlyr_create(image, drawable, title_color, inactive_color, active_color, \
        alarm_color, normal_color, active_diagram):

        txtlyrdict = get_extratxt_params( active_diagram )

        d_sorted_by_value = OrderedDict(sorted(txtlyrdict.items(), key=lambda x: x[1]))

        for k, txtlyr in d_sorted_by_value.items():
            #Assign Layer Text
            txtlayerName = txtlyr[3]
            #Assign Layer Font Name
            font_name = txtlyr[1]
            #Assign Font Size 
            font_size = txtlyr[2]

            #Assign Text color RGB values for colors are predefined constants because 
            #tuples don't pass correctly from xml

            if txtlyr[4] == "RED":
                textcolor = alarm_color
            elif txtlyr[4] == "WHITE":
                textcolor = inactive_color
            elif txtlyr[4] == "GREEN":
                textcolor = normal_color
            elif txtlyr[4] == "BLACK":
                textcolor = active_color

            #Assign Text X Coordinate
            xloc = txtlyr[7]
            #Assign Text Y Coordinate
            yloc = txtlyr[8]
            #Create the new text layer
            temp_layer = pdb.gimp_text_layer_new(image, txtlayerName, font_name, font_size, 0)
            #Add it to the image
            pdb.gimp_image_add_layer(image,temp_layer,-1)
            #Change the color of the text
            pdb.gimp_text_layer_set_color(temp_layer,textcolor)
            #Move the text to the proper location
            pdb.gimp_layer_translate(temp_layer, xloc, yloc)
            #Set the text justification
            pdb.gimp_text_layer_set_justification(temp_layer, 0)

テキストレイヤーが長すぎることがわかっている場合、テキストレイヤーに改行を追加するようにGIMPに指示するにはどうすればよいですか?

4

1 に答える 1

1

必要に応じて、を呼び出す前に、テキスト文字列に「改行」文字を追加するだけ pdb.gimp_text_layer_newです。

ところで、「txtlayerName」という名前を付けている変数 (変数の命名スタイルに関する考慮事項は別として) は、レイヤー名だけでなく、レイヤーの実際のテキスト コンテンツです。GIMP は通常、デフォルトでその内容によってテキスト レイヤーに名前を付けます。 が取得するパラメータgimp_text_layer_add_newは、画像上の実際のテキスト コンテンツです。代わりに、より完全な呼び出し : "pdb.gimp_text_fontname` を使用することを検討することもできます。これにより、必要なオフセットでレイヤーが画像に追加されます。

Python コードを使用して新しい行を挿入する場合は、文字列操作用の強力な言語構文とメソッドを使用するのと同じくらい簡単です。たとえば、レイヤーを作成する前に、多くのことを行うことができます。

このスニペットは、レイヤーの長さが 12 文字を超える場合はスペースを新しい行に置き換え、スペースがない場合は 6 文字ごとに任意にカットします。素朴ですが、アイデアを得ることができ、コードを作成できます。

if len(txtlayerName) > 12:
   if " " in txtlayerName:
       txtlayerName = txtlayerName.replace(" ", "\n")
   else:
       textlayerName = "\n".join(txtlayerName[i:i+6] for i in range(0, len(txtlayerName), 6))

このスニペットでは、各行に複数の単語があると仮定して、より長い行を使用できます。最大幅を 80 文字にしたい場合、テキスト内に空白で区切られた複数の単語があるとします。

textwidth = 80
result = []
txtline = []
index = 0
for word in textlayerName.split():
    if index + len(word) <  textwidth:
        txtline.append(word)
        index += len(word)
    else:
        result.append(" ".join(txtline))
        txtline = [word]
        index = len(word)

textlayerName = "\n".join(result)
于 2014-08-06T12:08:58.977 に答える