I am wondering whether there is a way to recognize (and possibly move/delete) B&W photos in folder containing both B&W and colored images? I am using Ubuntu Linux.


If you install ImageMagick, you can use the following command to test if something is greyscale:

$ convert bw.jpg -format "%[colorspace]" info:

To install:

sudo apt-get install imagemagick

So to go through them all and move:

for i in /images/folder ; do
  if [ "$(convert $i -format "%[colorspace]" info:)" == "Gray" ]; then
      mv "$i" /images/folder/bw

However, this method only tests for the colorspace that an image is using. An image might be using a full RGB colorspace, while only actually using greyscale tones (ideally these would be converted to greyscale to be optimal).

In order to work out is just using grey tones, one option is to convert the image to HSL colour, then calculate the average saturation of an image. For a true greyscale image, the average saturation will be zero. With greyscale images in jpg, you are going to get a bit of deviation from perfect greyscale due to artefacts, and generally they aren't perfect depending on how they ended up black and white.

This image for example:

black and white comics

If we convert this to HSL and get the average saturation:

$ convert black-and-white-comics.jpg -colorspace HSL -channel g \
>         -separate +channel -format "%[fx:mean]" info:

The figure output ranges from 0-1, so you would have to define a threshold under which you consider something to be greyscale depending on your source files.

  • 'convert bw.jpg -format "%[colorspace]" info:' gives a lot of false RGB results. – hnns Jun 6 '14 at 2:04
  • @hnns Can you post an example somewhere? – Paul Jun 6 '14 at 3:46
  • For example try this one: cristinabarkerjones.files.wordpress.com/2013/09/… – hnns Jun 6 '14 at 4:32
  • Hi @hnns, I have updated the answer – Paul Jun 6 '14 at 5:19
  • It would be cool if you explained a bit how the HSL command works: we can look the man pages of course, but it would be faster :-) Also the for i in /images/folder loop does not work for me on Bash 4.3. Did you mean for i in /images/folder/* ? – Ciro Santilli 新疆改造中心996ICU六四事件 Sep 26 '15 at 9:03

Using an Image Magick 7.0.8 installation under Windows 10, I had good success using the HSL conversion in the following batch script. It caught most of the B/W pictures, except those that are in sepia colors of course:

@echo off
if not exist bw md bw
for %%f in (*.jpg) do (
for /f %%i in ('magick "%%f" -colorspace HSL -channel g -separate +channel -format "%%[fx:mean]" info:') do set VAR=%%i
if !VAR! LEQ 0.05 move "%%f" .\bw)

In case you want to delete instead of moving the B/W pictures, just remove line 3 and replace the move "%%f" .\bw command in the last line with a del /Q "%%f"

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.