Because your are using a "f-string" (the f'{gimp.image_list()[0].filename}.txt') and this is only supported in Python3. In Gimp you are restricted to Python v2.7. You can use the quick & dirty %-formatting operator, something like:
Remark: Using gimp.image_list()[0] to obtain the image is not reliable. If you have more than one image loaded it won't work. If the plugin registration describes the first argument as as a PF_IMAGE then its is automatically set to the image on which the plugin is called and you can use directly in your code, than becomes:
You may also want to test that there is an image filename (using Image > Duplicate wipes it out, for instance).
Code:
def foo():
selection, x1, y1, x2, y2 = pdb.gimp_selection_bounds(gimp.image_list()[0])
if not selection:
pdb.gimp_message("No selection")
else:
with open(gimp.image_list()[0].filename+'.txt', 'w') as f:
res = '%d , %d, %d, %d' % (y1,x1,y2,x2)
f.writelines(res)
register(...)
main()
Remark: Using gimp.image_list()[0] to obtain the image is not reliable. If you have more than one image loaded it won't work. If the plugin registration describes the first argument as as a PF_IMAGE then its is automatically set to the image on which the plugin is called and you can use directly in your code, than becomes:
Code:
def foo(image):
selection, x1, y1, x2, y2 = pdb.gimp_selection_bounds(image)
if not selection:
pdb.gimp_message("No selection")
else:
with open(image.filename+'.txt', 'w') as f:
res = '%d , %d, %d, %d' % (y1,x1,y2,x2)
f.writelines(res)
register(
# some stuff here
[
(PF_IMAGE, "image", "Input image", None),
],
# More stuff here
)
main()
You may also want to test that there is an image filename (using Image > Duplicate wipes it out, for instance).