DEV Community

Cover image for Creating thumbnails with parallel and imagemagick
Sérgio Araújo
Sérgio Araújo

Posted on • Edited on

Creating thumbnails with parallel and imagemagick

Intro:

There are some unique commands that leaves us speechless and the imagemagick and gnu parallel are two of them. The imagemagick has a bunch of sub-commands for image manipulation, for example to create a image thumbnail of each image of your wallpapers folder you can run:

mkdir thumbs
mogrify -format jpg -path thumbs -thumbnail 200x200 *
Enter fullscreen mode Exit fullscreen mode

Some convertion operations, depending on the amout of files can consume a lot of processing and memory, so we can use gnu parallel to improve this task:

[ -d thumbs ] || mkdir thumbs
 fd -d1 -tf | parallel mogrify -format jpg -path thumbs -thumbnail 200x200 {}
Enter fullscreen mode Exit fullscreen mode

If the thumbs foldes does not exist we create it [ -d thumbs ] || mkdir thumbs, then the fd -d1 -tf createds the list of files, the gnu parallel will get each one of the entries and will apply the mogrify command.

The option -d1 ensures that fd will only search in the current folder.

Creating a README.md with thumbnails:

To use image thumbnails in your repo README.md file you must use this notation:

 [![image alt text](image URL link)](anchor link)
Enter fullscreen mode Exit fullscreen mode
fd -tf  . thumbs  | awk -F/ '{print "[!["$2"]("$1"/"$2")]("$2")"}' > README.md
Enter fullscreen mode Exit fullscreen mode

The trick here is to put awk literal strings between double quotes, just for the sake of test run this:

fd -tf . thumbs | awk -F/ '{print "field one="$1", field two="$2}'
fd -tf . thumbs | awk -F/ '{print $1" test "$2}'
Enter fullscreen mode Exit fullscreen mode

My wallpapers:

Just to see how it will end up being visit my wallpapers:

https://bitbucket.org/sergio/wallpapers/src/main/README.md

Top comments (1)

Collapse
 
emanoelopes profile image
Emanoel Lopes

Thanks for sharing.