Gimp-Forum.net
small plugin code help - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: small plugin code help (/Thread-small-plugin-code-help)

Pages: 1 2


small plugin code help - gimpygirl - 02-29-2024

Hi

I have this plugin that just runs some other scripts and plugins.

I want to add the first 2 comments lines. Can somebody tell me if this is possible and fill in the required code?
The plugin works but the 2 first comment lines I don't know how to code it.

First I want to add an alpha layer to my image.

Then I want to apply "select by color" tool on a specified pixel (X and Y value of the pixel) and delete the selection from  the whole image automatically? Is this possible using code?


Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from gimpfu import*


def test(timg, tdrawable):

#how to add alpha layer to my image?

#how to apply "select by color" tool on a specified pixel and delete the selection from  the whole image?

#determine the offset values manually
   pdb.gimp_drawable_offset(tdrawable, 0, 1, 18, -32)
   
   
#the image size is constant and 150x150 crops the border    
   pdb.gimp_image_crop(timg, 2400, 1200, 150, 150)
   
   
#guides using script-fu    
   pdb.script_fu_grid_guides(timg, 0, 150, 150, 1, 0)
   
   
#chop into 150x150 tiles    
   pdb.python_fu_ofn_guillotine_layer(timg, tdrawable)
   
   
#export all layers
   pdb.python_fu_ofn_export_layers(timg, os.path.dirname(timg.filename), "{numUp0}.png", "-", 0)
    
    
register(
       "test",
       "test",
       "test",
       "*",
       "*",
       "2024",
       "<Image>/Tools/test...",
       "*",
       [],
       [],
       test
       )

main()



RE: small plugin code help - Ofnuts - 02-29-2024

Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from gimpfu import *


def test(timg, tdrawable):
   timg.undo_group_start()   ### So that everything is undone with a single Ctrl-Z
   gimp.context_push()
   
   ### bracket the code in a try/except
   ### Any error is caught in the "except" and the script exits cleanly

   try:
       #how to add alpha layer to my image?
       ### pdb.gimp_layer_add_alpha(tdrawable)
       
       #how to apply "select by color" tool on a specified pixel and delete the selection from  the whole image?
       ### See pdb.gimp_image_select_color(image, operation, drawable, color)
       
       #determine the offset values manually
       pdb.gimp_drawable_offset(tdrawable, 0, 1, 18, -32)  ### These could be script parameters
       
       #the image size is constant and 150x150 crops the border    
       ###  IMHO the gimp_drawable_offset above is superfluous, you can combine both if you don't use 150,150
       pdb.gimp_image_crop(timg, 2400, 1200, 150, 150)  
       
       #guides using script-fu    
       pdb.script_fu_grid_guides(timg, 0, 150, 150, 1, 0)
       
       #chop into 150x150 tiles    
       pdb.python_fu_ofn_guillotine_layer(timg, tdrawable)
       
       #export all layers
       pdb.python_fu_ofn_export_layers(timg, os.path.dirname(timg.filename), "{numUp0}.png", "-", 0)

   except Exception as e:
       trace(e.args[0])
       gimp.message(e.args[0])
       if debug:
           traceback.print_exc()
   gimp.context_pop()
   timg.undo_group_end()

   
   
register(
      "gg-test",  # This one (aka "atom") should be unique across all scripts, so use a prefix
      "test",
      "test",
      "*",
      "*",
      "2024",
      "GGtest",   # Only menu entry here
      "*",
      [
       (PF_IMAGE,      'image',        'Input image',  None),  # Your "timg" arg
       (PF_DRAWABLE,   'layer',        'Input layer',  None),  # Your "tdrawable" arg
      ],
      [],
      test,
      menu="<Image>/Test" # Menu location here
      )

main()
You main sin was a space missing. It is "import *" (import everything), not "import*"

My own comments are prefixed with ###. Happy reading.


RE: small plugin code help - gimpygirl - 03-01-2024

If I change it to

Code:
from gimpfu import *

it still not appears in GIMP. What version you have?

I don't know how to make that line Sad

https://developer.gimp.org/api/2.0/libgimp/libgimp-gimpimageselect.html#gimp-image-select-color

How to make


Code:
GimpChannelOps operation


and

Code:
const GimpRGB *color


?
Can you please give example? I'm a beginner and it's too difficult.

When I have my script, I stop with this. Blush


RE: small plugin code help - Ofnuts - 03-01-2024

The API you point to is the C API. The one you use is all documented here:

[attachment=11334]

For the color part, you can use tuples (R,G,B)

Code:
# selecting blue (#0000FF)

# No decimal dot, integer format, [0 .. 255] range assumed
pdb.gimp_image_select_color(image, CHANNEL_OP_REPLACE, layer, (0,0,255))

# Decimal dot, floating point format, [0.0 .. 1.0] range assumed
pdb.gimp_image_select_color(image, CHANNEL_OP_REPLACE, layer, (0.,0.,1.))

# You can of course use a variable:
color=(0,0,255)
pdb.gimp_image_select_color(image, CHANNEL_OP_REPLACE, layer, color)



RE: small plugin code help - gimpygirl - 03-01-2024

I understand it now!!!

All the GIMPies JUMP JUMP!!!

All the GIMPies JUMP JUMP!!!

All the GIMPies JUMP JUMP!!!

gimpygirl is doing the Kirby Dance
WooooWoooWoooWoooWooooooooooooooooooooooooooooooooo

  • I used the color picker, but then I suddenly see 2 ranges: 0..100 and 0..255
What is the 0..100 range??? Sad 

So I just select a color with color picker and take the values of R, G and B in 0..255 range?
See screenshot: this color has 15, 15, 45 and then just this as RGB value in the code?

  • how to delete the selection from the image, made by gimp_image_select_color?



RE: small plugin code help - Ofnuts - 03-01-2024

[0 .. 100] is really 0% .. 100%, or in other words [0.0 .. 1.0] so 50.0 is more or less the same as 127.

[attachment=11340]



RE: small plugin code help - Ofnuts - 03-01-2024

(03-01-2024, 08:31 PM)gimpygirl Wrote:
  • how to delete the selection from the image, made by gimp_image_select_color?

You didn't search much, did you?

[attachment=11341]

(from that point of view using Gimp in English helps a lot because the API names are quite close to the names in the English UI...)



RE: small plugin code help - gimpygirl - 03-01-2024

(03-01-2024, 09:48 PM)Ofnuts Wrote:
(03-01-2024, 08:31 PM)gimpygirl Wrote:
  • how to delete the selection from the image, made by gimp_image_select_color?

You didn't search much, did you?



(from that point of view using Gimp in English helps a lot because the API names are quite close to the names in the English UI...)

Yes, I now see why I don't find anything
"clear" gives 0 results if you use another language! Sad
You can't even find it when using translated words...
So I think I switch gimp to English

It doesn't work if I use this for selecting red. in gimp the selected color doesn't change (i have image opened)


Code:
pdb.gimp_image_select_color(image, CHANNEL_OP_REPLACE, timg, (255,0,0))


or this


Code:
pdb.gimp_image_select_color(image, CHANNEL_OP_REPLACE, tdrawable, (255,0,0))

What parameter must be in there?
See my code in the beginning

When this line works, the foreground color here should appear as the color you put in the code, right?


RE: small plugin code help - Ofnuts - 03-02-2024

(03-01-2024, 11:18 PM)gimpygirl Wrote:
(03-01-2024, 09:48 PM)Ofnuts Wrote:
(03-01-2024, 08:31 PM)gimpygirl Wrote:
  • how to delete the selection from the image, made by gimp_image_select_color?

You didn't search much, did you?



(from that point of view using Gimp in English helps a lot because the API names are quite close to the names in the English UI...)

Yes, I now see why I don't find anything
"clear" gives 0 results if you use another language! Sad
You can't even find it when using translated words...
So I think I switch gimp to English

It doesn't work if I use this for selecting red. in gimp the selected color doesn't change (i have image opened)


Code:
pdb.gimp_image_select_color(image, CHANNEL_OP_REPLACE, timg, (255,0,0))


or this


Code:
pdb.gimp_image_select_color(image, CHANNEL_OP_REPLACE, tdrawable, (255,0,0))

What parameter must be in there?
See my code in the beginning

When this line works, the foreground color here should appear as the color you put in the code, right?

Code:
pdb.gimp_image_select_color(image, CHANNEL_OP_REPLACE, tdrawable, (255,0,0))

is correct, and it puts in the selection all the pixels that match red. But that doesn't update the foreground color... (if you use the By-color selection "manually" in the UI, it doesn't update the foreground color either). What you should see instead are the marching ants around the red areas.

[attachment=11346]



RE: small plugin code help - gimpygirl - 03-02-2024

But this function is tje same as "select by color" tool in gimp, right?
Because that is what i want.