Gimp-Forum.net
Does Script-Fu support input/output of the files? - 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)
+--- Thread: Does Script-Fu support input/output of the files? (/Thread-Does-Script-Fu-support-input-output-of-the-files)



Does Script-Fu support input/output of the files? - esm - 05-15-2024

Hi guys, I'm using Gimp 2.10.36 and need to find the coordinates of selected bounding box using this python code:

Code:
from gimpfu import *
import os

def foo():
   selection, x1, y1, x2, y2 = pdb.gimp_selection_bounds(gimp.image_list()[0])
   if not selection:
       pdb.gimp_message("No selection")
   else:
       gimp.message('%d, %d, %d, %d' % (y1, x1, y2, x2))
register(...)
main()

I'm wondering if there is a way to write the contents of a gimp.message(the coordinates) directly to a txt file? Can you please provide some examples?
This is the output I want to get - a created txt file with the same name as the image and 4 coordinates inside:
[attachment=11842]

This is the code I found but it's written in Scheme (Lisp dialect) and I don't understand how to rewrite it in python.
Code:
(define outport (open-output-file "samplefile.txt")) (display "Hello" outport)
(newline outport)
(display "World" outport)
(close-output-port outport)



RE: Does Script-Fu support input/output of the files? - Ofnuts - 05-15-2024

Plenty of examples of file I/O for Python out there. You can even write a nice CSV file directly (or add a library to write to Excel files).

My path-csv python script exports a path to a CSV file, so you can use it as an example (or use it directly if instead of using selections you use paths).

I'm not even sure that you can do proper file I/O in Script-fu, which is a very reduced dialect of Scheme.


RE: Does Script-Fu support input/output of the files? - esm - 05-16-2024

Thanks for the answer!
I looked your code through and noticed that you used with open() structure to write to csv file. 
I used it in my code, but it doesn't work as I see the button in the top menu bar through which the script is called has disappeared. From what I understand, this happens when there are syntax errors in the code. 
So I tested with open() part in a separate python file and it works but it doesn't work as a part of the code below:

Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gimpfu import *
import os

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(f'{gimp.image_list()[0].filename}.txt', 'w') as f:
          res = f'{y1}, {x1}, {y2}, {x2}'
          f.writelines(res)
register(...)
main()

Please tell me what I'm doing wrong


RE: Does Script-Fu support input/output of the files? - Ofnuts - 05-16-2024

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


RE: Does Script-Fu support input/output of the files? - esm - 05-16-2024

Wow, thank you very much! You helped me a lot