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, 17:36
Post: #2
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
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Gimp 2.10 with old vignette from Plugin registry. DenisT 2 84 May 19, 2018 20:42
Last Post: DenisT
  Export Layers Plugin not working with 2.10? Imc7r 2 196 Apr 30, 2018 14:22
Last Post: Imc7r
Question beautify plugin ! i can not install it! unknown7 3 402 Feb 12, 2018 21:02
Last Post: unknown7
  Separate+ plugin doesn't show NathanD 5 4,725 Jan 28, 2018 20:44
Last Post: janrich
  Any plugin for open multiple image in paint.net shinprog 1 641 Oct 31, 2017 11:38
Last Post: paynekj

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS