Reputation: 61
When I try to run the following python script through GIMP's gui:
#!/usr/bin/env python
from gimpfu import *
def export_png(image, drawable, self):
filename=image.name
interlace=0
compression=0
bkgd=0
gama=0
off=0
phys=0
time=0
pdb.file_png_save(image, drawable, filename, raw_filename, interlace, compression, bkgd, gama, offs, phys, time)
register(
"python_fu_export_png",
"Exports the current image as png",
"Exports the current image as png",
"HHP",
"HHP",
"2017",
"<Image>/Image/Export as png",
"*",
[
(PF_IMAGE, "image", "takes current image", None),
(PF_DRAWABLE,"drawable", "input layer",None),
],
[],
export_png
)
main()
I get this error:
Traceback (most recent call last):
File "/Applications/GIMP.app/Contents/Resources/lib/gimp/2.0/python/gimpfu.py", line 736, in response
dialog.res = run_script(params)
File "/Applications/GIMP.app/Contents/Resources/lib/gimp/2.0/python/gimpfu.py", line 361, in run_script
return apply(function, params)
TypeError: export_png() takes exactly 3 arguments (4 given)
Why is that? I thought I was only passing PF_Image and PF_DRAWABLE into the function, why does it say I'm giving four arguments?
Upvotes: 1
Views: 659
Reputation: 8904
If you replace your function by this:
def export_png(*parms):
for p in parms:
print type(p),p
it becomes clear that you get your parameters twice. I think this happens because you are using the deprecated form of registration:
"/Image/Export as png"
The current form of registration would be just "Export as png"
, and a named parameter at the end: menu="<Image>/Image"
(though IMHO this would be better put in <Image>/File/Export/
). When you use the current form, the first two parameters are filled implicitly with the current image and layer (if they have the right type), so there is no duplication. And in that case, they don't appear in the dialog, so if they are the only parms, the script is called directly without showing a dialog.
self
serves no purpose. It used in object methods to obtain a reference to the current object (and must be the first parameter). But this is not the case here, export_png()
is a plain function, not an object method.
Upvotes: 2