Fighting with computers

Computers are not always friendly.

Sunday, March 04, 2007

Grayscale with ImageMagick

I use command-line ImageMagick's graphics file handling software. It usually rocks and it use to be easy and simple to use, for that I love it. However, I've been fighting now for more than one hour to get it right.

What I wanted was to convert to grayscale a set of JPEG files I had on a folder. There are a couple of annoying things with ImageMagick:

  1. Once you use wildcard as the command source file (i.e. mogrify -resize 1024x768 *.JPG) then it seems to take a long long time to work. The reason seems to be the command reads and stores in RAM all the files BEFORE the magic starts. The result is a poor user response time (and sometimes an almost frozen system if you run out of memory and all the time is used paging). I use to change to use a for loop instead, with some "progress information" (i.e. for i in *.JPG; do mogrify -resize 1024x768 $i; echo $i " done!"; done )
  2. I do not convert to grayscale every week, so I cannot remember how to it. Here is when the trouble starts because the easy way is somehow obscure and several Google searches did not give the right information fast enough. The simple way seems to be mogrify -type Grayscale image.jpg but I was fooled instead to use the -colorspace parameter which produced no visible results but a full-color image file. So you know, next time you want to use this old tool you better write down your "magic" commands before it is too late.

12 Comments:

  • At 2:13 pm, Anonymous Anonymous said…

    Wow you really helped out me with your grayscale story. I had exactly the same f*cking problem. Thanks!

     
  • At 3:01 pm, Blogger Miguel Sánchez said…

    Glad to be useful. That's what I write it for.

    Thanks,

    Miguel

     
  • At 7:02 pm, Anonymous Anonymous said…

    Thanks, I also spent a while trying to use the -colorspace and other options to get a greyscale image.

    ImageMagick is great, but I wish it had vaguely complete documentation.

     
  • At 12:18 am, Anonymous Remi said…

    Thanks a lot! I spent 3h looking for other tools but finally found your blog. :)

     
  • At 5:38 am, Anonymous Simon said…

    nice post - but I have no trouble whatsoever using convert src.jpg -colorspace Gray out.jpg ????

     
  • At 8:54 am, Blogger Miguel Sánchez said…

    Lucky you!!

    I made this post as a reminder to myself after some frustration not finding the right way. Next time I'll give a try to your way.

    Thanks.

     
  • At 11:37 pm, Anonymous CableCat said…

    Here is the command to convert an image to grayscale using mogrify/ImageMagic:

    mogrify -modulate 100,0,50 image.jpg

     
  • At 11:10 am, Anonymous Tintazul said…

    For progress indication, try using the -verbose operator:

    mogrify -verbose -resize 1024x768 *.JPG

    It will tell what it's doing you as it goes along.

    About slurping all the images into RAM before processing, it doesn't do that: I routinely convert batches of 300 images of 10 MB each... but it's 2009 and I use Linux, so something may be different.

     
  • At 6:11 pm, Blogger Miguel Sánchez said…

    Thanks for the -verbose tip.

    I do use Linux too. Not sure what goes under the hood but it use to take a long time. I have not use it for a while and maybe the verbose option does help as you see that it's actually working.

     
  • At 12:44 am, Blogger Gustavo C said…

    to CableCat, about grayscale
    +1

     
  • At 9:45 pm, Blogger dan said…

    I am having trouble converting to grayscale from monochrome (indexed 2 color).

    I've dried using -type Grayscale, -colorspace Gray and the -modulate command listed earlier, and they all output an image that is still indexed 2 color monochrome.

    I need to convert to grayscale or RGB so that I can do some reasonable scaling and apply a blur. Does anyone know how to do this?

     
  • At 10:27 pm, Blogger Miguel Sánchez said…

    Please note that converting to Grayscale from monochrome will show you an image that looks exactly the same as the original one (but now each pixel may hold more than two different shades).

    Maybe you want to apply a blur once you've switch from monochrome to grayscale so then the change will become visible too.

    You can do a blur with imagemagik with -blur

     

Post a Comment

<< Home