Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python-fu basics: importing and exporting files
#1
Hi, I am sorry if this is a duplicate question. I did my best to check for topics that answered my question, but I'm still a little lost.

What I am trying to do:
load an image from folder_1,
do some transformation to it (this part I understand well enough),
save the image to folder_2 (I can do this using some code I found)

Where I am getting stuck:
my syntax for loading the file seems to be wrong.
It's not showing up in the file menu, which I assume means that I'm missing some essential lines.

I found some code that works to save the file, so I will omit that from my code sample.

This is basically what I have.



#!/usr/bin/python
from gimpfu import *

def load_file():

    path = "C:\Users\User\Pictures\GIMP\folder_1\01.png"
    filename = path
    raw_filename = path
    image = pdb.file_png_load(filename, raw_filename)


register(
    proc_name = ("python-fu-load"),
    blurb = ("load file"),
    help = ("imports png from 'new folder' with the name 01"),
    author = ("REC"),
    copyright = ("REC"),
    date = ("2022"),
    label = ("load file"),
    imagetypes = ("*"),
    parameters = [
        (PF_IMAGE, "image", "takes current image", None),
        (PF_DRAWABLE, "drawable", "input layer", None)
        ],
    results = [],
    function = (load_file),
    menu = ("<image>/Layer")
    )

main()




I know that I'm missing something very basic, but I haven't been able to find an example that simply loads a file to work on using python-fu.
I would love to get my hands on some basic utility plugins or examples written for python-fu, if I have something to reference I can probably figure out what I need.

Thank you for reading this.
Reply
#2
You are using keywords paramaters where you shouldn't:

Code:
Traceback (most recent call last):
 File "/home/me/Code/Gimp/NotMine/Activated/SaveLayer.py", line 27, in <module>
   menu = ("<image>/Layer")
TypeError: register() got an unexpected keyword argument 'parameters'

Also:
  • As a general rule, I find your use of register() disturbing. Most parameters are positional (see examples in linked page below).
  • There are way too many parentheses: copyright = "REC" works just as well.
  • In Python, like in all programming languages, the \ is an escape character, so where you need one as a plain character it should be doubled:  "C:\\Users\\User\\Pictures\\GIMP\\folder_1\\01.png". However:
    • In practice Windows will also accept forward slashes ("C:/Users/User/Pictures/GIMP/folder_1\01.png")
    • The right way to handle this is to
      1. Obtain the user's Pictures directory with gimp.user_directory(4) (this gives the good answer whatever the configuration and user's language on Windows, OSX  and Linux)
      2. Compose path using os.path.join()(*). For instance your path is os.path.join(gimp.user_directory(4),'GIMP','folder_1','01.png') (on all platforms).
      3. You can of course create a directory first (dir=os.path.join(gimp.user_directory(4),'GIMP','folder_1')) and then use it later in another join (os.path.join(dir,file))
To debug your Gimp python code see some hints and techniques there.

For some examples see here (especially those with export in the name)

(*) Of course you need to import os
Reply
#3
Thank you for your reply.
After cleaning up the mess in the register part of the code, I was able to get it to show up in the menu. So something there was causing the main problem. Looking at your working code is helping a lot, thank you for sharing that.
I will try to adopt the correct filepath handling going forward.
The debug post has been very helpful too, thanks for that as well.

I will study your library of plug-ins for awhile and try to rethink my approach.
Reply


Forum Jump: