我有一个运行良好的Python Gimp脚本,但有一个问题:
当我需要在图像上放置多行时,我必须创建两个单独的文本图层。
#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向文本图层添加换行符?
答案 0 :(得分: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)