Post Reply 

Attempting to Write Python Plugin and Failing Miserably

May 22, 2012, 00:42
Post: #1
Sad Attempting to Write Python Plugin and Failing Miserably
Hello Gimp Forums members,

I am attempting to write (what I thought would be) a simple plugin for GIMP that does three things:

1. Autocrops the image

2. Resizes the image (i.e. increases the canvas size without scaling the image) to make it square. I want this to occur by taking the shorter dimension and expanding it in both directions to make it equal to the length of the larger dimension (e.g. 120 x 80 px image becomes 120 x 120 px image)

3. Scales the image to a pre-defined size (e.g. 500 x 500 px)

So far, the following is the best I've been able to cobble together:

images = gimp.image_list()
layers = images[0].layers
gimp.pdb.plug_in_autocrop(images[0],layers[0])
w = gimp.pdb.gimp_image_width(images[0])
h = gimp.pdb.gimp_image_height(images[0])
if w > h:
#This resizes it if it is wide
gimp.pdb.gimp_image_resize(images[0],w,w,0,(w-h)/2)
else:
#This resizes it if it is tall
gimp.pdb.gimp_image_resize(images[0],h,h,(h-w)/2,0)


It wouldn't surprise me if that's a really ineffective way of getting what I want, but I have very little programming experience and no prior experience with Python.

I've been reading over many tutorials and resources on the web and unfortunately, this is as far as I've been able to get over multiple days of frustration.

Ideally I'd like to allow the plugin to prompt the user for a directory and have it perform these three operations on all of the images within. But at this point, I would greatly appreciate any help I can get.

P.S.

I tried to put what I've written so far into a Python skeleton, and although the plugin registers in GIMP, it does nothing to the image when I use it.
Find all posts by this user
Quote this message in a reply
May 22, 2012, 07:20
Post: #2
RE: Attempting to Write Python Plugin and Failing Miserably
If you register your plugin properly, and it is called from one of the main image window menus, the image it should work on is passed as a parameter (see for instance my own collection). But of course this is only valid for plugins called interactively.

Check for syntax errors. See also how you import the gimp module. The canonical way is to use:
Code:
from gimpfu import *
so that functions/members such as "pdb" can be called without prefixing them by "gimpfu" (and not "gimp"). So your code becomes something like:
Code:
from gimpfu import *

image=gimp.image_list()[0]
layer=image.layers[0]
pdb.plugin_autocrop(image,layer)

h=image.height
w=image.width
if h > w:
   # etc....

The problem with python under Windows is that you can't easily see the Python errors, or the output of "print" statements... I think using gimp_console can help. Doing a "dry run" of you python outside of Gimp 'python myplugin.py" will fail, but if the error is "No module named gimpfu" it means it didn't encounter major synatx errors (indenting, in particular). Running bits of code in the python-fu console can also catch many errors.

Take advantage of the better/more natural interface available in Python. Many things in the PDB are available as python class methods/attributes. It makes the code easier to read, and less prone to syntax errors.

Final note: if you have images with a single layer, see if by any chance you cannot do the same thing using ImageMagick, in batch mode it is somewhat easier to use than Gimp.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
May 22, 2012, 17:36
Post: #3
RE: Attempting to Write Python Plugin and Failing Miserably
Thanks for your reply!

I made the changes you suggested, and while the plug-in registers properly within GIMP, it fails with an error message when I attempt to use it:

"TypeError: autocropper_plus_function() takes exactly 2 arguments (4 given)"

Here's my script:

Code:
#!/usr/bin/env python

from gimpfu import *

def autocropper_plus_function(image,drawable):
    pdb.plug_in_autocrop(image,drawable)
    w = image.width
    h = image.height
    if w > h:
        #This resizes it if it is wide
        pdb.gimp_image_resize(image,w,w,0,(w-h)/2)
    else:
        #This resizes it if it is tall
        pdb.gimp_image_resize(image,h,h,(h-w)/2,0)

register(
    "Autocropper_Plus",
    "Autocropper Plus",
    "Autocrops the image and then increases the width or height to make it square",
    "Michael Varischetti",
    "Michael Varischetti",
    "May 2012",
    "<Image>/Image/Scripts/Autocropper Plus",
    "*",
    [
        (PF_IMAGE, "image", "Input image", None),
        (PF_DRAWABLE, "drawable", "Input drawable", None),
    ],
    [],
    autocropper_plus_function,
    )

main()

I'm assuming two of the arguments are "image" and "drawable" but I don't understand what the other two arguments are and where they are coming from.

Thanks in advance for any additional assistance you're willing to provide.
Find all posts by this user
Quote this message in a reply
May 22, 2012, 19:30
Post: #4
RE: Attempting to Write Python Plugin and Failing Miserably
(May 22, 2012 17:36)mikehardez Wrote:  Thanks for your reply!

I made the changes you suggested, and while the plug-in registers properly within GIMP, it fails with an error message when I attempt to use it:

"TypeError: autocropper_plus_function() takes exactly 2 arguments (4 given)"

Here's my script:

Code:
#!/usr/bin/env python

from gimpfu import *

def autocropper_plus_function(image,drawable):
    pdb.plug_in_autocrop(image,drawable)
    w = image.width
    h = image.height
    if w > h:
        #This resizes it if it is wide
        pdb.gimp_image_resize(image,w,w,0,(w-h)/2)
    else:
        #This resizes it if it is tall
        pdb.gimp_image_resize(image,h,h,(h-w)/2,0)

register(
    "Autocropper_Plus",
    "Autocropper Plus",
    "Autocrops the image and then increases the width or height to make it square",
    "Michael Varischetti",
    "Michael Varischetti",
    "May 2012",
    "<Image>/Image/Scripts/Autocropper Plus",
    "*",
    [
        (PF_IMAGE, "image", "Input image", None),
        (PF_DRAWABLE, "drawable", "Input drawable", None),
    ],
    [],
    autocropper_plus_function,
    )

main()

I'm assuming two of the arguments are "image" and "drawable" but I don't understand what the other two arguments are and where they are coming from.

Thanks in advance for any additional assistance you're willing to provide.
Register with;
Code:
register(
    "Autocropper_Plus",
    "Autocropper Plus",
    "Autocrops the image and then increases the width or height to make it square",
    "Michael Varischetti",
    "Michael Varischetti",
    "May 2012",
    "Autocropper Plus",
    "*",
    [
        (PF_IMAGE, "image", "Input image", None),
        (PF_DRAWABLE, "drawable", "Input drawable", None),
    ],
    [],
    autocropper_plus_function,
    menu="<Image>/Image/Scripts",
    )

The two extra parameters you have are the Image & Layer from the dialog that pops up, because your registration syntax makes it consider your Image/Layer parameters as added to the implicit ones. Don't ask me why, there are some subtleties there which I never really dug into. Note that with the syntax above, you don't get a popup, since your script has all it needs from the implicit parameters.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Loading PythonFu Plugin: ImportError for .../python/gimp.so (GIMP 2.8.22/OSX/Py2.7.9) joroek 1 199 Jul 11, 2017 07:26
Last Post: ythgilb
  [Easy] Sobel plugin parameters t4u 2 272 Oct 7, 2014 19:09
Last Post: ofnuts
  Color menuitems in script or plugin Barbie 4 474 Sep 29, 2014 09:51
Last Post: paynekj
  Setting text markup from python script Burbly 1 351 Sep 16, 2014 12:29
Last Post: Burbly
  Tried to write first script piotrkast 2 321 Sep 15, 2014 21:08
Last Post: piotrkast

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS