It is currently Thu Aug 13, 2026 12:06 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Plugin is not found in the menus
PostPosted: Fri Feb 07, 2025 4:57 pm  (#1) 
Offline
GimpChat Member

Joined: Dec 19, 2018
Posts: 164
Hello,
I would like to test this plugin but it does not appear in any menu in GIMP.
Even searching via Help - Plug-in Browser - Search I cannot find the plugin.

Could any Python programmer analyze this code and check if the way it addresses the plugin location is correct?

Code below in spoiler.

Thanks in advance

#!/usr/bin/env python

from gimpfu import *

def blend_if(img, drawable, this_layer_low, this_layer_high, underlying_layer_low, underlying_layer_high):
# Verifica se a imagem e a camada são válidas
if not pdb.gimp_item_is_valid(drawable):
return

# Obtém a camada atual
current_layer = pdb.gimp_image_get_active_layer(img)

# Obtém a camada subjacente
underlying_layer = None
for layer in img.layers:
if layer != current_layer:
underlying_layer = layer
break

if underlying_layer is None:
return

# Processa os pixels da camada atual e da camada subjacente
width = pdb.gimp_drawable_width(drawable)
height = pdb.gimp_drawable_height(drawable)

# Define o modo de processamento dos pixels
pdb.gimp_context_set_default_colors()
pdb.gimp_context_set_opacity(100)
pdb.gimp_context_set_paint_mode(GIMP_NORMAL_MODE)

# Itera sobre todos os pixels
for y in range(height):
for x in range(width):
pixel_current = pdb.gimp_drawable_get_pixel(current_layer, x, y)
pixel_underlying = pdb.gimp_drawable_get_pixel(underlying_layer, x, y)

# Converte os pixels para valores de cor (R, G, B)
r_current, g_current, b_current = pixel_current[0:3]
r_underlying, g_underlying, b_underlying = pixel_underlying[0:3]

# Calcula a luminância dos pixels
lum_current = 0.299 * r_current + 0.587 * g_current + 0.114 * b_current
lum_underlying = 0.299 * r_underlying + 0.587 * g_underlying + 0.114 * b_underlying

# Aplica a lógica do Blend-If
if this_layer_low <= lum_current <= this_layer_high and underlying_layer_low <= lum_underlying <= underlying_layer_high:
# Mistura os pixels
r_blend = int((r_current + r_underlying) / 2)
g_blend = int((g_current + g_underlying) / 2)
b_blend = int((b_current + b_underlying) / 2)
pixel_blend = (r_blend, g_blend, b_blend, 255)
pdb.gimp_drawable_set_pixel(current_layer, x, y, pixel_blend)

# Atualiza a camada
pdb.gimp_displays_flush()

# Registro do plugin
register(
"blend-if",
"Plugin para simular a funcionalidade Blend-If do Photoshop",
"Aplica a lógica do Blend-If nas camadas selecionadas",
"Seu Nome",
"Seu Nome",
"2023",
"<Image>/Filters/Blend-If",
"*",
[
(PF_IMAGE, "image", "Input image", None),
(PF_DRAWABLE, "drawable", "Input drawable", None),
(PF_INT, "this_layer_low", "This Layer Low", 0),
(PF_INT, "this_layer_high", "This Layer High", 255),
(PF_INT, "underlying_layer_low", "Underlying Layer Low", 0),
(PF_INT, "underlying_layer_high", "Underlying Layer High", 255),
],
[],
blend_if,
menu="<Image>/Filters"
)

main()


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: Plugin is not found in the menus
PostPosted: Fri Feb 07, 2025 7:57 pm  (#2) 
Offline
GimpChat Member
User avatar

Joined: Jan 13, 2011
Posts: 2648
Location: Poland
You must publish as "code" or provide a source link. :hehe

_________________
Image


Top
 Post subject: Re: Plugin is not found in the menus
PostPosted: Sat Feb 08, 2025 4:03 am  (#3) 
Offline
GimpChat Member

Joined: Dec 19, 2018
Posts: 164
MareroQ wrote:
You must publish as "code" or provide a source link. :hehe


Hello MareroQ!

I'm sorry but I don't understand what "publish as "code" or provide a source link" means. :roll:

I'm really a noob when it comes to plugin code.

Let me clarify the situation here.

Some friends and I were discussing which AIs (GPT, DeepSeek and Qwen) would be the best to use.

So we subjected them to the same tasks and compared the results.

Since I've always admired this effect in PS, I decided to test if these AIs would be able to produce Blend-If for GIMP. But I wasn't even able to find the plugin after installing it.

I even tried to tweak the code. I made some intuitive changes, reported the problem to the AI, but in the end I was still in the same situation as before.

It's possible that the code won't even work even if I manage to address it correctly so that it appears in the Filters menu.

In any case, I tried, I did what I could.

MareroQ, thank you very much for answering and contributing to my question! :bigthup


Top
 Post subject: Re: Plugin is not found in the menus
PostPosted: Sat Feb 08, 2025 9:31 am  (#4) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4812
Python is sensitive to code indentation.

if condition:
    print("this is printed if condition is true")
    print("this is also printed only if condition is true")
print("this is always printed")


The code you show is like this:

if condition:
print("this printed if condition is true")
print("this also printed only if condition is true")
print("this is always printed")


So 1) python complains that it doesn't see an indent after the "if condition" line and 2) figuring out which line(s) should be re-indented requires some serious reverse-engineering/debugging.

So either the code you have has already lost its indentation and you are in trouble or it still has its indentation and then provide it in way that preserves it; either using \[code\] \[/code\] tags as I did above, or by adding the Python file as an attachment.

_________________
Image


Top
 Post subject: Re: Plugin is not found in the menus
PostPosted: Sun Feb 09, 2025 2:19 am  (#5) 
Offline
GimpChat Member

Joined: Dec 19, 2018
Posts: 164
ofnuts wrote:
Python is sensitive to code indentation.

if condition:
    print("this is printed if condition is true")
    print("this is also printed only if condition is true")
print("this is always printed")


The code you show is like this:

if condition:
print("this printed if condition is true")
print("this also printed only if condition is true")
print("this is always printed")


So 1) python complains that it doesn't see an indent after the "if condition" line and 2) figuring out which line(s) should be re-indented requires some serious reverse-engineering/debugging.

So either the code you have has already lost its indentation and you are in trouble or it still has its indentation and then provide it in way that preserves it; either using \[code\] \[/code\] tags as I did above, or by adding the Python file as an attachment.

Hi Ofnuts,
I should have noticed that the code was missing indentation when I pasted it here in this post.

After your observation, I was wondering if the lack of indentation was already generated by the AI ​​or if I lost it at some point between pressing ctrl+c and ctrl+v.

I checked and found that the AI ​​generated the code correctly indented.

So, while I was typing this text, I decided to check if the code in the plugins folder was indented or not. Yes, it has the correct formatting!

I'm going to paste the code again and if during this process it loses its formatting (indentation), I'll attach the .py.

#!/usr/bin/env python

from gimpfu import *

def blend_if(img, drawable, this_layer_low, this_layer_high, underlying_layer_low, underlying_layer_high):
# Verifica se a imagem e a camada são válidas
if not pdb.gimp_item_is_valid(drawable):
return

# Obtém a camada atual
current_layer = pdb.gimp_image_get_active_layer(img)

# Obtém a camada subjacente
underlying_layer = None
for layer in img.layers:
if layer != current_layer:
underlying_layer = layer
break

if underlying_layer is None:
return

# Processa os pixels da camada atual e da camada subjacente
width = pdb.gimp_drawable_width(drawable)
height = pdb.gimp_drawable_height(drawable)

# Define o modo de processamento dos pixels
pdb.gimp_context_set_default_colors()
pdb.gimp_context_set_opacity(100)
pdb.gimp_context_set_paint_mode(GIMP_NORMAL_MODE)

# Itera sobre todos os pixels
for y in range(height):
for x in range(width):
pixel_current = pdb.gimp_drawable_get_pixel(current_layer, x, y)
pixel_underlying = pdb.gimp_drawable_get_pixel(underlying_layer, x, y)

# Converte os pixels para valores de cor (R, G, B)
r_current, g_current, b_current = pixel_current[0:3]
r_underlying, g_underlying, b_underlying = pixel_underlying[0:3]

# Calcula a luminância dos pixels
lum_current = 0.299 * r_current + 0.587 * g_current + 0.114 * b_current
lum_underlying = 0.299 * r_underlying + 0.587 * g_underlying + 0.114 * b_underlying

# Aplica a lógica do Blend-If
if this_layer_low <= lum_current <= this_layer_high and underlying_layer_low <= lum_underlying <= underlying_layer_high:
# Mistura os pixels
r_blend = int((r_current + r_underlying) / 2)
g_blend = int((g_current + g_underlying) / 2)
b_blend = int((b_current + b_underlying) / 2)
pixel_blend = (r_blend, g_blend, b_blend, 255)
pdb.gimp_drawable_set_pixel(current_layer, x, y, pixel_blend)

# Atualiza a camada
pdb.gimp_displays_flush()

# Registro do plugin
register(
"blend-if",
"Plugin para simular a funcionalidade Blend-If do Photoshop",
"Aplica a lógica do Blend-If nas camadas selecionadas",
"Seu Nome",
"Seu Nome",
"2023",
"<Image>/Filters/Blend-If",
"*",
[
(PF_IMAGE, "image", "Input image", None),
(PF_DRAWABLE, "drawable", "Input drawable", None),
(PF_INT, "this_layer_low", "This Layer Low", 0),
(PF_INT, "this_layer_high", "This Layer High", 255),
(PF_INT, "underlying_layer_low", "Underlying Layer Low", 0),
(PF_INT, "underlying_layer_high", "Underlying Layer High", 255),
],
[],
blend_if,
menu="<Image>/Filters"
)

main()


Thank you very much for your help!

PS.: Oh.. I noted the following when creating this post:
When I paste the code in the text area between s poil.../s poil, and then preview it, everything is correct, the indentation is present! But after submitting the post for publication somehow the indentation is lost.

Attachment:
Indentation02.png
Indentation02.png [ 158.13 KiB | Viewed 3987 times ]


Attachments:
blend-If.7z [1.15 KiB]
Downloaded 45 times
Top
 Post subject: Re: Plugin is not found in the menus
PostPosted: Sun Feb 09, 2025 2:28 am  (#6) 
Offline
GimpChat Member
User avatar

Joined: Dec 09, 2018
Posts: 736
Don't use spoiler tags, use code like this:

blah
    indented blah


The code icon is two to the left of the spoiler icon.
Or you can just type the tags.


Top
 Post subject: Re: Plugin is not found in the menus
PostPosted: Sun Feb 09, 2025 5:05 am  (#7) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4812
These damn foreigners:


python blend-If.py
  File "blend-If.py", line 6
SyntaxError: Non-ASCII character '\xc3' in file blend-If.py on line 6, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details


Add this as the second line:

# -*- coding: UTF-8 -*-


This said, looking at the code, 1) it should be very slow (can be improved somewhat using "pixels regions" and 2) I wonder how different this is from this:

* Copy the two layers
* Blend them (top layer at 50% opacity then merge down)
* Apply over the original bottom layer in lighten only mode.

More accurate procedure:

* Duplicate both layers and apply "Desaturate > Luminosity" to each
* Set the top of these to "Grain extract" and merge
* Threshold the result to 128 or .5, this gives you a map of the pixels where the top layer is more luminous than the bottom one.
* In the Channels, do a "Channel to selection" on any of the RGB layers
* Back to the original top layer, add a layer mask and initialize to selection
* Set the opacity of that layer to 50%

Can of course be scripted, but on any large image it could be faster manually than running the script.

_________________
Image


Top
 Post subject: Re: Plugin is not found in the menus
PostPosted: Mon Feb 10, 2025 6:18 am  (#8) 
Offline
GimpChat Member

Joined: Dec 19, 2018
Posts: 164
ofnuts wrote:
These damn foreigners:


python blend-If.py
  File "blend-If.py", line 6
SyntaxError: Non-ASCII character '\xc3' in file blend-If.py on line 6, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details


Add this as the second line:

# -*- coding: UTF-8 -*-


This said, looking at the code, 1) it should be very slow (can be improved somewhat using "pixels regions" and 2) I wonder how different this is from this:

* Copy the two layers
* Blend them (top layer at 50% opacity then merge down)
* Apply over the original bottom layer in lighten only mode.

More accurate procedure:

* Duplicate both layers and apply "Desaturate > Luminosity" to each
* Set the top of these to "Grain extract" and merge
* Threshold the result to 128 or .5, this gives you a map of the pixels where the top layer is more luminous than the bottom one.
* In the Channels, do a "Channel to selection" on any of the RGB layers
* Back to the original top layer, add a layer mask and initialize to selection
* Set the opacity of that layer to 50%

Can of course be scripted, but on any large image it could be faster manually than running the script.

Great! It worked!
This UTF-8 line solved the problem!

The more accurate procedure is interesting. I will test it again in other editing situations.
But I can't compare it with the result of the code since it didn't work for me.

Anyway, you can see that the code didn't recreate the Blend-If as desired, since the preview makes a lot of difference to me.

teapot wrote:
Don't use spoiler tags, use code like this:

blah
    indented blah


The code icon is two to the left of the spoiler icon.
Or you can just type the tags.

I saw this icon, but I thought it was for indentation or numeric list, so I didn't even click on it.
Although I think I've used this code here before, I just don't remember if the icon image was the same.


teapot and ofnuts, thx a lot!


Top
 Post subject: Re: Plugin is not found in the menus
PostPosted: Mon Feb 10, 2025 2:46 pm  (#9) 
Offline
GimpChat Member
User avatar

Joined: Dec 09, 2018
Posts: 736
ofnuts wrote:
More accurate procedure:

* Duplicate both layers and apply "Desaturate > Luminosity" to each
* Set the top of these to "Grain extract" and merge
* Threshold the result to 128 or .5, this gives you a map of the pixels where the top layer is more luminous than the bottom one.
* In the Channels, do a "Channel to selection" on any of the RGB layers
* Back to the original top layer, add a layer mask and initialize to selection
* Set the opacity of that layer to 50%

Hi ofnuts, Thank you for the procedure.

I was just wanting to try it but how do I apply "Desaturate > Luminosity"?

Under the menu Colours -> Desaturate -> Desaturate the modes I see are:
Luminance
Luma
Lightness (HSL)
Average (HSI Intensity)
Value (HSV)


Top
 Post subject: Re: Plugin is not found in the menus
PostPosted: Mon Feb 10, 2025 4:06 pm  (#10) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4812
teapot wrote:
ofnuts wrote:
More accurate procedure:

* Duplicate both layers and apply "Desaturate > Luminosity" to each
* Set the top of these to "Grain extract" and merge
* Threshold the result to 128 or .5, this gives you a map of the pixels where the top layer is more luminous than the bottom one.
* In the Channels, do a "Channel to selection" on any of the RGB layers
* Back to the original top layer, add a layer mask and initialize to selection
* Set the opacity of that layer to 50%

Hi ofnuts, Thank you for the procedure.

I was just wanting to try it but how do I apply "Desaturate > Luminosity"?

Under the menu Colours -> Desaturate -> Desaturate the modes I see are:
Luminance
Luma
Lightness (HSL)
Average (HSI Intensity)
Value (HSV)


Luminance is fine (actually the formula used in the script is called Luminance in some sources). In practice it shouldn't be too important since all these values increase in the same direction so comparing their luminance, lightness, or luma, shouldn't change the result.

_________________
Image


Top
 Post subject: Re: Plugin is not found in the menus
PostPosted: Tue Feb 11, 2025 12:17 am  (#11) 
Offline
GimpChat Member
User avatar

Joined: Dec 09, 2018
Posts: 736
ofnuts wrote:
Luminance is fine (actually the formula used in the script is called Luminance in some sources). In practice it shouldn't be too important since all these values increase in the same direction so comparing their luminance, lightness, or luma, shouldn't change the result.

Thanks ofnuts :tyspin


Top
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]



* Login  



Powered by phpBB3 © phpBB Group