Tips and Tricks About Computers, Web Development, Linux, the Internet and the Like
Bash script to convert pngs to jpgs, thumbnail them and generate forum code
I’ve been working on a tutorial for Nexuiz Ninjaz that requires a lot of screen shots. I’ve been utilizing the command line tool scrot to take screenshots on specific windows. These images however, are large pngs because the scrot date pattern wasn’t working properly when I tried to generate jpgs while repeating the same command. I went forward knowing I could find a way to batch compress these pngs later. Figuring I might as well “add a turbo while I’m under the hood”, I opted to both thumbnail the images (duh) and generate the forum thumbnail code (VROOOM).
So, to convert pngs to jpgs, I created a bash script which uses mogrify to convert, compress and thumbnail them and sed to assist in generating the forum code.
#!/bin/bash
#
# Convert pngs to jpgs, thumbnail them and generate forum code
# Created by Tyler Mulligan - www.doknowevil.net
condir=converted
if [ ! -d $condir ];then mkdir -p $condir;fi
pagename="http://www.nexuizninjaz.com/pics/howtos/map_link_a_ninja/"
# Convert to jpg and create thumbnails
echo -e "backing up originals...\n"
cp *.png originals
echo -e "converting to jpg...\n"
mogrify -format jpg *.png
echo -e "copying to converted directory...\n"
cp *.jpg $condir
echo -e "creating thumbnails...\n"
mogrify -resize 50% *.jpg
for file in *.jpg ; do mv $file `echo $file | sed 's/\(.*\.\)jpg/thumb_\1jpg/'` ; done
mv *.jpg $condir
echo -e "cleaning up\n"
rm *.png
# Generate forum code
for file in $condir/thumb_*.jpg ; do
tempname=`echo $file | sed 's/thumb_\(.*\)/\1/'`;
echo "[url=${pagename}${tempname}][img]thumb_${tempname}[/img][/url]"
done
Future plans call for adding flags to support different image types, and including the ability to take screen shots and generate the forum code on a per screen shot basis.
Sorta Off Topic: I’ve taken a swing at frameworks be developing a new layer of organization on top of cfg files in nexuiz. I’ve thoroughly documented this scalable, fully customizable package I’ve dubbed the Ninja Pack for Nexuiz
| Print article | This entry was posted by Tyler Mulligan on August 11, 2008 at 11:18 pm, and is filed under Bash, Freeware, Linux, Nexuiz, Programming. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 3 years ago
Nice article. Thanks. :) Eugene
about 2 years ago
Nice one, really useful. For more number of files handling you can use:
FILES=(/root/demo/*.jpg)
and then
for FILE in “${FILES[@]}”; do
operation on $FILE
; done
Regards,
http://unstableme.blogspot.com/search/label/bash%20scripts
about 10 months ago
One again, your idea is very
good.thank you!very much.