Could someone write a command line for channels swapping and etc. please?

Since I’ve only recently came to software and documentation is so huge, still can’t figure it out how to do stuff. I need to automatically batch process image, and doing the stuff in GIMP manually is tedious. So, there are two images: input and output. Both have RGBA channels. I need to do as follows:

  1. Copy input’s alpha channel to output’s red
  2. Copy input’s blue channel to output’s green
  3. Copy input’s green channel to output’s blue
  4. Copy input’s red channel to output’s alpha
  5. Curve level output’s red channel with input 255 and output 180 (or multiply each pixels by 0.707)
  6. Value invert output’s green channel
  7. Curve level output’s blue channel with input 0 and output 128 (basically, need to make pixels values scaled to 128-255 range)
  8. Save output image on disc

Thanks in advance.

@eksdee Welcome to the forum. It took me a while to learn how G’MIC works. May I ask which version you are using? I am using the dev version of 2.9.0pre, so the syntax may vary if you are using an ancient version. Basic workflow.

1-4 Separate and reverse channel order.
gmic input.png split c reverse

4-7 Indexing operations. After the reversal, images are in R,G,B,A order and so will be [0],[1],…
mul[0] {180/255} negate[1] normalize[2] 128,255

8 Combine channels and save.
append c output output.png

If you are looking to do a batch operation, you could wrap it with a repeat $! local[$>] ... endlocal done loop. $! means total number of images and $> counts up the image index. Combined with input_glob at the beginning, which can glob a certain set of images from your folder, you have a working command.

2 Likes

Maybe this could help :

4 Likes

it should works with the fill command

identity image
-fill [i0#0,i1#0,i2#0,i3#0]

the right command
-fill [i3#0*0.707,255-i2#0,((i1#0)/2)+128,i0#0]

1 Like

Thank you very much! After manual tweaks it works just like intended!

1 Like

Something which is probably not so easy is to think about the easiest/simplest way to do something in G’MIC. There are many possibilities of doing the same thing, and as with any other programming language, the script writer has to figure out what technique suits the most his needs.

1 Like

I agree and didn’t say otherwise.

I know :slight_smile: The fact you proposed a completely different approach to the OP problem made me realize this. That’s interesting to see different approaches from different people.

Well, the results are different depending on the interpretation. It so happens that your solution was what @eksdee was hoping for. :bellhop_bell:x3

Here’s an alternative approach to this problem.

Copy input’s alpha channel to output’s red
$ gmic input.png sh[0] 0 f. i3#-2 rm.

Copy input’s blue channel to output’s green
$ gmic input.png sh[0] 1 f. i2#-2 rm.

Copy input’s green channel to output’s blue
$ gmic input.png sh[0] 2 f. i1#-2 rm.

Copy input’s red channel to output’s alpha
$ gmic input.png sh[0] 2 f. i1#-2 rm.

Curve level output’s red channel with input 255 and output 180 (or multiply each pixels by 0.707)
$ gmic input.png sh[0] 0 *. .707 rm.

Value invert output’s green channel
$ gmic input.png sh[0] 1 f. 255-i rm.

Curve level output’s blue channel with input 0 and output 128 (basically, need to make pixels values scaled to 128-255 range)
$ gmic input.png sh[0] 1 f. i/2+128 rm.

Save output image on disc
$ gmic input.png sh[0] 1 f. i/2+128 rm. o output.png

You could remove[0] by the way if you have only one image. Otherwise, repeat $! l[$>] endl done would be needed using this approach.