Page 1 of 1

Embarrassingly simple question :?

Posted: 2010-05-10T22:28:42-07:00
by smaines
My ambition was brute simple: reverse the blue and green channels in an image. Gnome users may recognize its file name blue_type.png as a desktop background image.

I tried several variations, none of which seemed correct. Also, these two inexplicably yielded different results,

Code: Select all

$ convert blue_type.png -channel b -fx "u.g" -channel g -fx "u.b" green_type.png
$ convert blue_type.png -channel g -fx "u.b" -channel b -fx "u.g" green_type.png
I also tried (again inexplicably different results),

Code: Select all

$ convert blue_type.png blue_type_2.png -channel b -fx "v.g" -channel g -fx "v.b" green_type.png
$ convert blue_type.png blue_type_2.png -channel g -fx "v.b" -channel b -fx "v.g" green_type.png
...just in case u refers to the in-process clone (?!). Also I tried,

Code: Select all

$ convert blue_type.png -channel gb -fx "channel(r,b,g,a)" green_type.png
And many other things too silly to recall here.

What I expected to work was something like,

Code: Select all

$ convert blue_type.png  -fx "out.g = in.b;out.b = in.g"  green_type.png
But there seem to be no such lvalue semantics in -fx.

I am in awe of what this package does, and very embarrassed to be falling down at the first hurdle in trying to use it. What am I missing here?

Thanks in advance,

-SM

Re: Embarrassingly simple question :?

Posted: 2010-05-11T08:28:20-07:00
by fmw42
try

convert image -alpha off -colorspace rgb -separate -swap 1,2 -combine -alpha on result


For example:

convert -size 100x100 xc:blue -alpha off -colorspace rgb -separate \
-swap 1,2 -combine -alpha on tmp.png

will convert a blue image into a green image

Re: Embarrassingly simple question :?

Posted: 2010-05-11T08:35:20-07:00
by snibgo

Code: Select all

$ convert blue_type.png -channel b -fx "u.g" -channel g -fx "u.b" green_type.png
Should first set the blue channel to be equal to the green, then set the green channel to be equal to the new blue channel. It's like trying to swap the value in two variables by:

Code: Select all

a=b;
b=a;
which won't work.

Try:

Code: Select all

convert blue_type.png \( +clone -channel G -fx B \) \
  +swap -channel B -fx v.G     green_type.png
See http://www.imagemagick.org/Usage/transform/

about half way down (search for "swap the").

Re: Embarrassingly simple question :?

Posted: 2010-05-11T11:09:43-07:00
by fmw42
in general -fx is slower that just using non-fx commands such as -separate and -combine.

Re: Embarrassingly simple question :?

Posted: 2010-05-11T11:18:00-07:00
by snibgo
Yes, as mentioned on that transform page.

convert blue_type.png -separate -swap 1,2 -combine green_type.png

Re: Embarrassingly simple question :?

Posted: 2010-05-11T12:08:11-07:00
by smaines
I've just returned to my computer. Thank you all for your replies, I so needed the sanity check! =D

I have a few comments:

snibgo, I understand what you're saying about a=b;b=a, (given the apparent, regretable implicit behavior of u), but then why doesn't this one work?

Code: Select all

    $ convert blue_type.png blue_type_2.png -channel b -fx "v.g" -channel g -fx "v.b" green_type.png
    $ convert blue_type.png blue_type_2.png -channel g -fx "v.b" -channel b -fx "v.g" green_type.png
Generally, though, shouldn't the solution to this problem have simpler expression within the semantics of -fx? Even if -separate,-swap,-combine is more efficient in this case, it is not intuitive, really, nor is resorting to -clone. (I confess I skipped over that part of the Transforms page, thinking it was too complex to be the right answer for something so simple). It seems like -fx is begging for a predefined symbol for "out"- and no funny business with u1 being implicitly mutable! Just an observation.

I'm looking forward to exploring ImageMagick further. Cool stuff!

Thanks again for all your help,

-SM

Re: Embarrassingly simple question :?

Posted: 2010-05-11T12:58:20-07:00
by snibgo
I think separate/combine, and even clone/swap/delete become intuitive with use. I once worked as a draughtsman/tracer, which included sliding transparent sheets around and copying from one to another. Layers in Gimp, and image sequences in IM, work in much the same way.

I use "fx" only as a last resort. It is slow and not a general programming language. For example, I can have a conditional, but only one.


If blue_type_2.png is a copy of blue_type.png, this will work:

Code: Select all

convert blue_type_.png blue_type_2.png -channel B -fx v.g -channel g -fx v.b -delete 1 green_type.png
This is what you had, but with the addition of "-delete 1". This command creates a sequence of image zero and image one. Then it modifies the blue and green channels of image zero. Then it removes image one, so image zero is copied to the output. Without that delete, it would copy the unmodified image one.

(We don't need to create blue_type_2.png. We can just use +clone.)

Re: Embarrassingly simple question :?

Posted: 2010-05-11T17:31:33-07:00
by anthony
NOTE: -fx removes all images except for the 'result' image. Please keep this in mind!

As such the correct -fx solution for swapping green and blur channels is

Code: Select all

  convert rose: \( +clone -channel G -fx B \) \
          +swap -channel B -fx v.G     fx_gb_swap.gif

Another way of 'swapping' image channels is to use a -recolor (or -color-matrix) matrix.
however I have yet to review the changes to these options after the breif wave of 'fixes' with color matrix handling. (I was very busy at that time)

The simplest method for speed and understandability is the -separate -swap -combine technique.

Re: Embarrassingly simple question :?

Posted: 2010-05-12T05:11:35-07:00
by snibgo
Thanks Anthony.
NOTE: -fx removes all images except for the 'result' image. Please keep this in mind!
I don't understand. Do you mean that in:

convert blue_type_.png blue_type_2.png -channel B -fx v.g -channel g -fx v.b -delete 1 green_type.png

the first fx will remove image 1 (blue_type_2.png), so the second fx won't work, and the "-delete 1" should do nothing? Because this command does work. (Of course, it isn't the best way of doing the job.)

Re: Embarrassingly simple question :?

Posted: 2010-05-12T18:32:24-07:00
by snibgo
Yes, the documentation (http://www.imagemagick.org/Usage/transform/ ) says "The command takes a image sequence of as many input images you like. Typically one or two images, and replaces ALL the input images with a copy of the first image, which has been modified by the results of the "-fx" function."

So the command I gave shouldn't work.

Re: Embarrassingly simple question :?

Posted: 2010-05-12T18:55:07-07:00
by anthony
This is why my 'channel swap' example (above) uses parenthesis and a clone. so that the original image is preserved.