Grayscale All Images in a Directory With Imagemagick


Posted by Jason Hardin on October 31, 2012

Below is a shell script to grayscale all gif images in a directory and save them as pngs using ImageMagick. I used this to convert all Moodle plugin icons to grayscale for Moodlerooms grayscale icon packs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash

update (){
	cd $1
	pwd
	for i in `ls $1`
	do
		if [ -d $1/$i ]
		then
			echo "Updating $i "
			cd $i
			for f in `ls .`;
			do
				convert $f -colorspace Gray ${f%.gif}.png
				rm $f
			done
			cd ..
		fi
	done
}

if [ "$1" == "" ]; then
	update .
else
	update $1
fi