It is currently Mon Apr 15, 2024 7:42 am


All times are UTC - 5 hours [ DST ]


Switch to mobile style

Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Same operation on all open images
PostPosted: Tue Mar 21, 2017 12:50 am  (#1) 
Offline
New Member

Joined: Mar 21, 2017
Posts: 4
Hi,
I'm attempting my first script, I've gone through a tutorial, successfully edited and ran a simple script on a single image.

Now I want to write a script which when run will operate on all open images. Is there a function which returns an array of all image objects? I did find a function that returns all image_ids, and that is pdb.gimp_image_list(). But I need the object it seems, as an input to the function I am wanting to perform.

The code:
for tempimage in pdb.gimp_image_list():
pdb.plug_in_unsharp_mask(tempimage, drawable, radius, amount, threshold)
pdb.gimp_desaturate_full(tempimage.active_drawable, desaturate_mode)

Returns an error that the wrong type is being used in the unsharp mask function, tempimage. I need the object and not the id. Any suggestions?

Thanks
David J.


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: Same operation on all open images
PostPosted: Tue Mar 21, 2017 1:33 am  (#2) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
as Tin said under Gimp-Learn:
You could try
gimp.image_list()
it'll return all images as objects currently opened that you can use.

_________________
"Where am I ?"


Top
 Post subject: Re: Same operation on all open images
PostPosted: Tue Mar 21, 2017 4:00 am  (#3) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4734
I concur with dinasset, gimp.image_list() returns a list of gimp.Image objects.

Maybe show us a bit more code. The "wrong type" message could be for another parameter. What are the contents of the other variables (drawable, radius, amount, threshold).

_________________
Image


Top
 Post subject: Re: Same operation on all open images
PostPosted: Tue Mar 21, 2017 2:12 pm  (#4) 
Offline
New Member

Joined: Mar 21, 2017
Posts: 4
gimp.imagelist() is exactly what I'm trying to use in the snippet above. I'm editing the plugin from a working exercise plugin, which operates on one image and works.
The working code that works on one image:

#!/usr/bin/env python

from gimpfu import *

def extreme_unsharp_desaturation(image, drawable, radius, amount, desaturate_mode):
    # Unsharp mask from filters menu
    pdb.gimp_image_undo_group_start(image)   
    threshold = 0
    pdb.plug_in_unsharp_mask(image, drawable, radius, amount, threshold)
    pdb.gimp_desaturate_full(drawable, desaturate_mode)   
    pdb.gimp_image_undo_group_end(image)

register(
    "python-fu-extreme-unsharp-desaturation",
    "Unsharp mask and desaturate image",
    "Run an unsharp mask with amount set to 5, then desaturate image",
    "Jackson Bates", "Jackson Bates", "2015",
    "Extreme unsharp and desaturate...",
    "RGB", # type of image it works on (*, RGB, RGB*, RGBA, GRAY etc...)
    [
        # basic parameters are: (UI_ELEMENT, "variable", "label", Default)
        (PF_IMAGE, "image", "takes current image", None),
        (PF_DRAWABLE, "drawable", "Input layer", None),
      (PF_SLIDER, "radius", "Radius", 5, (0, 500, 0.5)),
      (PF_SLIDER, "amount", "Amount", 5.0, (0,10,0.1)),
      (PF_RADIO, "desaturate_mode", "Set Desaturation Mode: ", DESATURATE_LIGHTNESS,
          (
             ("Lightness", DESATURATE_LIGHTNESS),
            ("Luminosity", DESATURATE_LUMINOSITY),
            ("Average", DESATURATE_AVERAGE)
          )
      )
    ],
    [],
    extreme_unsharp_desaturation, menu="<Image>/Filters/Enhance")  # second item is menu location

main()


The entire code that gets the error:

#!/usr/bin/env python

from gimpfu import *

def extreme_unsharp_desaturation_multi(image, drawable, radius, amount, desaturate_mode):
    pdb.gimp_image_undo_group_start(image)   
    num_images, image_ids = pdb.gimp_image_list() #function returns multiple values
    threshold = 0
    for tempimage in pdb.gimp_image_list():
        pdb.plug_in_unsharp_mask(tempimage, drawable, radius, amount, threshold)
        pdb.gimp_desaturate_full(tempimage.active_drawable, desaturate_mode)   
    pdb.gimp_image_undo_group_end(image)

register(
    "python-fu-extreme-unsharp-desaturation-multi",
    "Unsharp mask and desaturate image",
    "Run an unsharp mask with amount set to 5, then desaturate image",
    "Jackson Bates", "Jackson Bates", "2015",
    "Extreme unsharp and desaturate multi...",
    "RGB", # type of image it works on (*, RGB, RGB*, RGBA, GRAY etc...)
    [
        # basic parameters are: (UI_ELEMENT, "variable", "label", Default)
        (PF_IMAGE, "image", "takes current image", None),
        (PF_DRAWABLE, "drawable", "Input layer", None),
      (PF_SLIDER, "radius", "Radius", 5, (0, 500, 0.5)),
      (PF_SLIDER, "amount", "Amount", 5.0, (0,10,0.1)),
      (PF_RADIO, "desaturate_mode", "Set Desaturation Mode: ", DESATURATE_LIGHTNESS,
          (
             ("Lightness", DESATURATE_LIGHTNESS),
            ("Luminosity", DESATURATE_LUMINOSITY),
            ("Average", DESATURATE_AVERAGE)
          )
      )
    ],
    [],
    extreme_unsharp_desaturation_multi, menu="<Image>/Filters/Enhance")  # second item is menu location

main()


The contents of the other variables are populated by dialog or constants.

And the error:
Traceback (most recent call last):
File "C:\Program Files\GIMP 2\32\lib\gimp\2.0\python\gimpfu.py", line 736, in response
dialog.res = run_script(params)
File "C:\Program Files\GIMP 2\32\lib\gimp\2.0\python\gimpfu.py", line 361, in run_script
return apply(function, params)
File "C:\Users\david\.gimp-2.8\plug-ins\Extreme_Unsharp_Desaturation_multi.py", line 15, in extreme_unsharp_desaturation_multi
pdb.plug_in_unsharp_mask(tempimage, drawable, radius, amount, threshold)
TypeError: wrong parameter type


Top
 Post subject: Re: Same operation on all open images
PostPosted: Tue Mar 21, 2017 3:11 pm  (#5) 
Offline
GimpChat Member
User avatar

Joined: Jan 20, 2013
Posts: 14816
Location: roma, italy
You use that statement, but then you ignore the results.
The result is a tuple (as you wrote): the number of images and the list of the images.
You have to scan the list of the images you've got (what you have called "images_ids"), not the tuple.

_________________
"Where am I ?"


Top
 Post subject: Re: Same operation on all open images
PostPosted: Tue Mar 21, 2017 4:49 pm  (#6) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4734
Some remarks:
  • Don't use pdb.gimp_image_list() (which returns id) but gimp.image_list() (that returns gimp.Image objects)
  • Image and drawable args are not mandatory
  • You code loops applying unsharp-mask to the same drawable (the one from the image from which you call the script)
  • You need undo-group pairs on each image
  • "python-fu" is added autmatically

So:

#!/usr/bin/env python

from gimpfu import *

def extreme_unsharp_desaturation_multi(radius, amount, desaturate_mode):
    threshold = 0
    for tempimage in gimp.image_list():
        pdb.gimp_image_undo_group_start(tempimage)   
        pdb.plug_in_unsharp_mask(tempimage, tempimage.active_drawable, radius, amount, threshold)
        pdb.gimp_desaturate_full(tempimage.active_drawable, desaturate_mode)   
        pdb.gimp_image_undo_group_end(tempimage)

register(
    "extreme-unsharp-desaturation-multi",
    "Unsharp mask and desaturate image",
    "Run an unsharp mask with amount set to 5, then desaturate image",
    "Jackson Bates", "Jackson Bates", "2015",
    "Extreme unsharp and desaturate multi...",
    "RGB", # type of image it works on (*, RGB, RGB*, RGBA, GRAY etc...)
    [
        # basic parameters are: (UI_ELEMENT, "variable", "label", Default)
      (PF_SLIDER, "radius", "Radius", 5, (0, 500, 0.5)),
      (PF_SLIDER, "amount", "Amount", 5.0, (0,10,0.1)),
      (PF_RADIO, "desaturate_mode", "Set Desaturation Mode: ", DESATURATE_LIGHTNESS,
          (
             ("Lightness", DESATURATE_LIGHTNESS),
            ("Luminosity", DESATURATE_LUMINOSITY),
            ("Average", DESATURATE_AVERAGE)
          )
      )
    ],
    [],
    extreme_unsharp_desaturation_multi, menu="<Image>/Filters/Enhance")  # second item is menu location

main()

_________________
Image


Top
 Post subject: Re: Same operation on all open images
PostPosted: Wed Mar 22, 2017 11:47 pm  (#7) 
Offline
New Member

Joined: Mar 21, 2017
Posts: 4
dinasset wrote:
You use that statement, but then you ignore the results.
The result is a tuple (as you wrote): the number of images and the list of the images.
You have to scan the list of the images you've got (what you have called "images_ids"), not the tuple.


No I didn't ignore it, I tried scanning image_ids in addition to pdb.gimp_image_list(), got same error. As I should, they are equal. The subsequent poster has the answer, use gimp.image_list, not gimp_image_list, that actually returns objects not ids. I didn't read the original suggestion to do this closely enough earlier in the thread.


Top
 Post subject: Re: Same operation on all open images
PostPosted: Wed Mar 22, 2017 11:48 pm  (#8) 
Offline
New Member

Joined: Mar 21, 2017
Posts: 4
Thanks ofnuts I'll give that shot.


Top
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Unable to open images in GIMP

31

No new posts Open images in RGB mode automatically

1

No new posts Attachment(s) Trying to open as layers 3 til files Red, Green, Blue, Filter images

2

No new posts [solved] Tool/GEGL operation

4

No new posts Attachment(s) Open Sky

3



* Login  



Powered by phpBB3 © phpBB Group