Posts: 150
Threads: 31
Joined: Jan 2017
Reputation:
8
Operating system(s):
I have been trying to write a script to automate colour cast removal.
If an image has transparency it "dilutes" the colour cast I am trying to remove.
When I run the following code
Code:
layerClean = pdb.gimp_layer_new_from_drawable(layer, image) # layer to be cleaned of alpha channel
pdb.gimp_image_insert_layer(image, layerClean, None, 0) # insert layer
pdb.gimp_image_set_component_visible(image, CHANNEL_ALPHA, FALSE) # make alpha channel non-visible
pdb.gimp_layer_flatten(layer) # remove alpha channel
pdb.gimp_image_remove_layer(image, layerClean)
I can look in the channels palette and the alpha channel has gone. In the layers palette I can right-click on the layer and it gives me the option to add an alpha channel. However, if I carry out any action, such as adding a layer, the alpha channel re-appears in the channel palette.
I have tried running the code and then doing a "Save as". On loading the saved file, the alpha channel is again in the layers palette.
I created an xcf file where the alpha channel was filled with 50% grey (7f7f7f Hex) in order to test.
I put this into general queries as the same occurs when the actions are carried out manually.
Operating system: Ubuntu 20.04
GIMP: 2.10.32
Posts: 6,339
Threads: 272
Joined: Oct 2016
Reputation:
562
Operating system(s):
Gimp version: 2.10
07-26-2022, 04:11 PM
(This post was last modified: 07-26-2022, 04:12 PM by Ofnuts.)
The alpha channel in Channels exists as soon as a layer has an alpha channel (even if fully opaque). However this channel is virtual, it is the composite of all the alpha channels of the visible layers and in itself it has little influence over the output. What you really work with is the alpha channel of individual layers, so you don't have to worry about that one.
Posts: 150
Threads: 31
Joined: Jan 2017
Reputation:
8
Operating system(s):
@Ofnuts,
I probably was not clear with my explanation. I know that the alpha channel in the channels palette is largely virtual.
When I clear the alpha channel with the code above the channels palette is as in this image.
In the layers palette I can right-click and it gives me the option - Add Alpha Channel. If I add a new layer, or if I save and reload the image it shows a 50% grey alpha channel in the layers palette - the same alpha channel that I removed with the above code. But right-clicking in the layers palette still gives me the option Add Alpha Channel. It appears to be a "ghost" alpha channel. If it was the default, I would expect it to be white and I would not have the option Add Alpha Channel.
Running my routine for removing colour cast on the original image and the one to which I added the 50% alpha layer gives different results which suggests that the colour cast has been diluted by the "ghost" alpha channel.
Posts: 6,339
Threads: 272
Joined: Oct 2016
Reputation:
562
Operating system(s):
Gimp version: 2.10
This still means that your problem is with a layer, not with the whole image.
The real problem is likely in the code before this. Can you show the whole code?
Posts: 150
Threads: 31
Joined: Jan 2017
Reputation:
8
Operating system(s):
Code:
from gimpfu import *
def python_c_c_r(image,layer):
pdb.gimp_image_undo_group_start(image)
pdb.gimp_context_push()
width = image.width
height = image.height
type = RGB_IMAGE
opacity = 100
mode = NORMAL_MODE
layerClean = pdb.gimp_layer_new_from_drawable(layer, image) # layer to be cleaned of alpha channel
pdb.gimp_image_insert_layer(image, layerClean, None, 0) # insert layer
pdb.gimp_image_set_component_visible(image, CHANNEL_ALPHA, FALSE) # make alpha channel non-visible
pdb.gimp_layer_flatten(layer) # remove alpha channel
pdb.gimp_image_remove_layer(image, layerClean)
pdb.gimp_context_pop()
pdb.gimp_image_undo_group_end(image)
pdb.gimp_displays_flush()
register(
"python_fu_c_c_r",
"c_c_r",
"c_c_r",
"David Marsden",
"David Marsden",
"July 2022",
"<Image>/Python-Fu/c_c_r...", #Menu path
"RGB*, GRAY*, INDEXED*",
[],
[],
python_c_c_r)
main()
Here is the total code. The same as you have seen already, apart from the parameters of the original image.
The subsequent code would create a new layer, pixelated to the maximum dimensions of the image, colour inverted and changed to overlay mode.
Posts: 1,511
Threads: 69
Joined: May 2021
Reputation:
167
Operating system(s):
Gimp version: 2.10
07-27-2022, 01:58 AM
(This post was last modified: 07-27-2022, 03:09 AM by PixLab.)
(07-26-2022, 08:14 PM)david Wrote:
Code:
from gimpfu import *
def python_c_c_r(image,layer):
pdb.gimp_image_undo_group_start(image)
pdb.gimp_context_push()
width = image.width
height = image.height
type = RGB_IMAGE
opacity = 100
mode = NORMAL_MODE
layerClean = pdb.gimp_layer_new_from_drawable(layer, image) # layer to be cleaned of alpha channel
pdb.gimp_image_insert_layer(image, layerClean, None, 0) # insert layer
pdb.gimp_image_set_component_visible(image, CHANNEL_ALPHA, FALSE) # make alpha channel non-visible
pdb.gimp_layer_flatten(layer) # remove alpha channel
pdb.gimp_image_remove_layer(image, layerClean)
pdb.gimp_context_pop()
pdb.gimp_image_undo_group_end(image)
pdb.gimp_displays_flush()
register(
"python_fu_c_c_r",
"c_c_r",
"c_c_r",
"David Marsden",
"David Marsden",
"July 2022",
"<Image>/Python-Fu/c_c_r...", #Menu path
"RGB*, GRAY*, INDEXED*",
[],
[],
python_c_c_r)
main()
Here is the total code. The same as you have seen already, apart from the parameters of the original image.
The subsequent code would create a new layer, pixelated to the maximum dimensions of the image, colour inverted and changed to overlay mode.
Oh... It's for that thread, that's very nice from you, BTW that's a cool trick with the big pixel averaging the color cast
I'm not programmer, but if I may... I would take/compare the longest side of the image after taking the width and the height (on your code) to pixelate at the longest side+1 to sure sure to pixelate a big one pixel to the whole image whatever is portrait or paysage.
Maybe something like that? (don't forget, no programmer here, no python knowledge and so, please forgive syntax and error)
Code:
width = image.width
height = image.height
type = RGB_IMAGE
opacity = 100
mode = NORMAL_MODE
if (width >= height) {
pixelSize = width+1;
}
else {
pixelSize = height+1;
}
// pixelSize is to use on the pixelization filter to scale the size of the pixel on both sliders
Also, do you really need to "touch" the alpha channel?
If a picture has an alpha channel but is opaque (like this photo on your first post), the pixelisation will work just fine.
If a picture has an alpha channel with some transparencies parts, removing the alpha channel will fill the transparency with the active BG color, thus if the active BG color was set to something exotic(or not), when the pixel will average the color cast, it will take in count the BG fill of the transparency as well, thus the averaging will be off, no?
Just a thought
Posts: 150
Threads: 31
Joined: Jan 2017
Reputation:
8
Operating system(s):
@PixLab,
All credit for using pixelate to average the colour of an image must go to Krikor. He came up with the idea and Tin Tran programmed it into a plug-in.
I decided that I could create a plug-in with a little more flexibility by adding a desaturated layer of the original image in overlay mode (initially set at 50% opacity) which allows subsequent adjustment of contrast. This works quite well as a one-click way of removing a colour cast.
The next step was to make it work for images with an alpha channel (and ideally re-instate the alpha channel on completion), because transparency "dilutes" the effect of the colour cast reduction. This was where my problems started!
You say that you are not a programmer - I suspect you have more programming knowledge than I do. My only programming experience was some 40 years ago, in assembly language on 8 and 16 bit processors, which was simple and logical!
I attach the plug-in which works OK for images without alpha channel in case it is of interest to you.
col-cast-reduction.zip (Size: 1.27 KB / Downloads: 116)
Posts: 7,132
Threads: 155
Joined: Oct 2016
Reputation:
1,003
Operating system(s):
Gimp version: 2.10
Dare I mention but Ofnuts has a good averaging plugin ofn-average-fill.py dated 2017-01-09
...from the usual place http://sourceforge.net/projects/gimp-too...s/scripts/ (hate those smart links, spent endless hours telling my sister not to click on them)
Posts: 150
Threads: 31
Joined: Jan 2017
Reputation:
8
Operating system(s):
Update!
By copying my original layer into a new RGB layer (and I delete the original) I get the same result on images with or without alpha channel.
I have yet to find out how to save the alpha channel and re-insert it at the end of the program.
The latest version:
col-cast-reduction.zip (Size: 1.52 KB / Downloads: 241)
Posts: 6,339
Threads: 272
Joined: Oct 2016
Reputation:
562
Operating system(s):
Gimp version: 2.10
Tried with an image of mine and results with and without an alpha channels are identical.
Now, there is an alpha channel, and there are partially opaque pixels, you can have the first without the second.
So if you problem is that partially opaque pixels in the copies ( layer_copy and layer_copy1) are skewing the result of the contrast/color layers overlay, you can just make these two layers totally opaque by using :
Code:
if layer.has_alpha:
pdb.plug_in_threshold_alpha(image,layer,0)
|