I am working in opencv. right now I have a task to decompose images into only red channel. I can make it by using GIMP. It goes like this:

Colors -> Components -> Decompose (color model RGB)

This way I get 3 new grayscale images, one with green channel, one with red and one with blue channel. I only need grayscale image with red channel. Anyways, problem is I have few thousand pictures in my folder named "positives" to convert, so if I do it this way it would take so much time.

Is there any way to write a script and run it from terminal to convert images into red channel and save them all in folder at once?

I guess it doesn't even need to be a long script, it is probably a semi-simple command.

Please help and thanks in advance


If we need processing of many files we may want to use ImageMagick command line tools convert or mogrify:

convert input.png -channel R -separate output.png

R is for red channel. Imagemagick also works on multiple files using wildcards or other channels (G,B ...).

The following command is for many files that would not fit into the RAM. It will overwrite all existing image files in the current directory with a red channel greyscale. Do not work on originals!

mogrify * -channel R -separate *
  • Takkat you're the boss! Thank you very much, works like charm :D – Painkiller Jul 13 '17 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.