Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Concatenate a layer name with fixed text and variables
#2
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))
  • The things after the % in the format string ("Blur %3.1f, contrast %2d") are format specifications:
    • 3.1f means at least 3 characters wide, at least one decimal and expects a floating point value
    • %2d means integer (base 10), at least 2 characters wide (%d would mean: just what is needed), and expects an integer
    • You can also find %s for string (or whatever the __str__ method returns for an object.
  • Format specs are replaced by the values at the same position in the tuple that follows the % operator ((blur,contrast)). If you have only one format value you can skip the tuple.

In Pythonv3 you could use an "f-string":
Code:
pdb.gimp_item_set_name(new_layer,f'Blur: {blur:3.4f}, contrast: {contrast:d}')
but so far in Gimp you have to stick to Python v2.
Reply


Messages In This Thread
RE: Concatenate a layer name with fixed text and variables - by Ofnuts - 02-11-2023, 06:08 PM

Forum Jump: