Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Does Script-Fu support input/output of the files?
#4
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:

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).
Reply


Messages In This Thread
RE: Does Script-Fu support input/output of the files? - by Ofnuts - 05-16-2024, 11:50 AM

Forum Jump: