Concatenate a layer name with fixed text and variables - Printable Version +- Gimp-Forum.net (https://www.gimp-forum.net) +-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP) +--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP) +---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions) +---- Thread: Concatenate a layer name with fixed text and variables (/Thread-Concatenate-a-layer-name-with-fixed-text-and-variables) |
Concatenate a layer name with fixed text and variables - tkemmere - 02-11-2023 Hi all, I'm learning Python as I go along in the Gimp. Can somebody get me going on this one please? I have a few variables loaded with figures. (blur is for example 10 and contrast 0.15). I would like to set a layer name, so that later I can save the layer with a filename based on the layer name. But how do I combine fixed text and variables? It must be something like this, but how exactly? Code: pdb.gimp_item_set_name(new_layer, "Blur "+{blur}+"contrast "+{contrast}) I would like the layer name to be "Blur 10 contrast 0.15". I also tried with "&" but that didn't work either. Nor did it without the "{}". Am I close? Thanks! RE: Concatenate a layer name with fixed text and variables - Ofnuts - 02-11-2023 What you want to do is called string formatting. There are several forms in Python v2, and even more forms in Pythin v3. The simplest form uses the percent operator: Code: pdb.gimp_item_set_name(new_layer, "Blur %3.1f, contrast %2d" % (blur,contrast))
In Pythonv3 you could use an "f-string": Code: pdb.gimp_item_set_name(new_layer,f'Blur: {blur:3.4f}, contrast: {contrast:d}') RE: Concatenate a layer name with fixed text and variables - tkemmere - 02-12-2023 Thank you Offnuts! More complex than I expected, but... ...I understand why that is so, cause this way offers much versatility ...well explained by you! I got it going almost immediately! Code: ... |