A bash one-liner for resizing images

I have always ejoyed ImageMagick – in particular (much like gdal) I get a wierd kind of pleasure from being able to edit images from something so innately non-graphical as the command line. One common activity I do is resizing images. In this case, I wanted every image in a directory to be 200px wide whilst it’s height should be proportionately resized to match the width.

I also wanted to remove pesky spaces from the file names, and make sure they were all writable. Here is a little one liner I came up with:

sudo chmod a+w *.jpg; for FILE in *; do mv "$FILE" $(echo $FILE | sed 's/ /_/g'); \
echo $FILE; done; for FILE in *.jpg; do echo $FILE; convert -resize 200x $FILE $FILE; done

I know it could have been done more efficiently in a single loop but since I was knocking this together in a jiff, it wouldn’t have been worth the time…

The real take-home command is

convert -resize 200x $FILE $FILE

which resizes the file to 200px wide and then saves it over itself.

pixelstats trackingpixel

Submit a Comment

You must be logged in to post a comment.