Posts: 3
Threads: 1
Joined: May 2024
Reputation:
0
Operating system(s):
- Windows Vista or 7, 8, 10 (64-bit)
Gimp version: 2.10
05-15-2024, 09:38 PM
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:
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)
Posts: 6,366
Threads: 276
Joined: Oct 2016
Reputation:
565
Operating system(s):
Gimp version: 3.00RC1
05-15-2024, 11:23 PM
(This post was last modified: 05-15-2024, 11:24 PM by Ofnuts.)
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.
Posts: 3
Threads: 1
Joined: May 2024
Reputation:
0
Operating system(s):
- Windows Vista or 7, 8, 10 (64-bit)
Gimp version: 2.10
05-16-2024, 09:45 AM
(This post was last modified: 05-16-2024, 10:19 AM by esm.)
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
Posts: 6,366
Threads: 276
Joined: Oct 2016
Reputation:
565
Operating system(s):
Gimp version: 3.00RC1
05-16-2024, 11:50 AM
(This post was last modified: 05-16-2024, 11:50 AM by Ofnuts.)
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).
Posts: 3
Threads: 1
Joined: May 2024
Reputation:
0
Operating system(s):
- Windows Vista or 7, 8, 10 (64-bit)
Gimp version: 2.10
Wow, thank you very much! You helped me a lot
|