Gimp python script from current image

1.2k views Asked by At

I'm trying to get to grips with image manipulation using GIMP and I'm failing at the first hurdle:

With an image loaded in GIMP, I want to run a registered script to run a function - simply rotate the whole image by 90 degrees and then display a message.

#!/usr/bin/env python
 
import os
import sys
import time
from gimpfu import *

def simple_test():
  num = 90
  image = gimp.pdb # not sure if this is calling the active image
  drawable = pdb.gimp_image_get_active_layer(image)

  rotate_it(image, 90)


def rotate_it(image, deg):
  msg = "simple_test!! " + str(deg) + "\n"
  pdb.gimp_image_rotate(gimp.pdb, num)
  gimp.message(msg)

 
register(
    "simple_test",
    "A simple Python-Fu plug-in",
    "When run this plug-in rotates an image 90 degrees",
    "Ghoul Fool",
    "Ghoul Fool",
    "2020",
    "simple test",
    "",
    [],
    [],
    simple_test,
    menu="<Image>/Filters/simple-test",
)
 
main() 

More importantly is trying to get some error message/log/console output to find out where I'm going wrong - only that doesn't seem to display by default.

2

There are 2 answers

0
paddy.exe On

it definitely looks like you are calling the current image wrongly. Perhaps this example script can help you out:

#!/usr/bin/env python

# Tutorial available at: https://www.youtube.com/watch?v=nmb-0KcgXzI
# Feedback welcome: [email protected]

from gimpfu import *

def hello_warning(image, drawable):
    pdb.gimp_message("Hello world!")
    

register(
    "python-fu-hello-warning",
    "SHORT DESCRIPTION",
    "LONG DESCRIPTION",
    "Jackson Bates", "Jackson Bates", "2015",
    "Hello warning",
    "", # type of image it works on (*, RGB, RGB*, RGBA, GRAY etc...)
    [
        (PF_IMAGE, "image", "takes current image", None),
        (PF_DRAWABLE, "drawable", "Input layer", None)
    ],
    [],
    hello_warning, menu="<Image>/File")  # second item is menu location

main()

I would also suggest this guy's video series about gimp python fu: https://www.youtube.com/watch?v=nmb-0KcgXzI. Unfortunately, the official documentation (in my opinion) lacks beginner friendliness considering the debugging.

0
G M On

The image is always the first parameter of the function of your plug-in and it will be derived from the context so you don't need it to get it before, as paddy.exe showed your plug-ing should have it as the first argument:

def hello_warning(image, drawable):
    pdb.gimp_message("Hello world!") 

For getting the image if you want to do some testing using the console you should use:

image = gimp.image_list()[0]