Resize and Watermark Images in Linux

Posted by admin on December 30, 2007 under Tech Tips | 8 Comments to Read

If you need to resize and watermark large number of images, there is an easy way to do so using Linux command line tools and a very basic shell script. The easiest and most straight-forward method you can use is through the use of the ImageMagick toolkit. If you don’t have it installed already, you can download it from their site. Most distributions have binaries already built for you. Ubuntu and Debian users can install ImageMagick from the main repositories.

Install ImageMagick

sudo apt-get install imagemagick

Prepare your working environment

Make a directory to store the watermark image (~/Pictures/watermark) and photos you’re working on (~/Pictures/temp). Copy the watermark image and original photos to their respective directories.

mkdir -p ~/Pictures/temp/
mkdir -p ~/Pictures/watermark/
cp /path/to/watermark.jpg ~/Pictures/watermark/
cp /path/to/original-photos/*.jpg ~/Pictures/temp/

ls -l ~/Pictures/watermark/
-rw-r--r-- 1 gmendoza gmendoza 3311 2007-12-30 17:35 watermark.jpg

ls -l ~/Pictures/temp
-rw-r--r-- 1 gmendoza gmendoza 885788 2007-12-30 17:35 ubuntu1.jpg
-rw-r--r-- 1 gmendoza gmendoza 128922 2007-12-30 17:31 ubuntu2.jpg

Change directories to begin working on your photos.

cd ~/Pictures/temp/

Resizing with “convert”

Let’s say you want to specify a maximum width of 440 pixels (height being adjusted proportionally) in order for you to post the images appropriately within the specifications of your website borders. The syntax would be the following:

convert -resize 440 original-image.jpg new-image.jpg

In this example, we will specify the same name for the output files, which will overwrite the original copies.

convert -resize 440 ubuntu1.jpg ubuntu1.jpg
convert -resize 440 ubuntu2.jpg ubuntu2.jpg

Watermark with “composite”

You will probably want to watermark the images after they have been resized, this way there is no distortion of the watermark, and it is appropriately sized. In this example, we will also set the watermark transparency level to 15%, and overwrite the original file again.

composite -gravity northeast -dissolve 15 ../watermark/watermark.jpg
ubuntu1.jpg ubuntu1.jpg
composite -gravity northeast -dissolve 15 ../watermark/watermark.jpg
ubuntu2.jpg ubuntu2.jpg

Your photos will now have a nice watermark in their upper right hand corners.

Automating the process with a script

You can automate these steps and apply them to a large number of files using a script of course. Feel free to download this one and modify it to your liking.

echo "*****************************************"
echo "* Image Resize and Watermarking Script  *"
echo "* By Gilbert Mendoza -! *"
echo "*****************************************"
echo " "
for each in ~/Pictures/temp/*{.jpg,.jpeg,.png}
  echo "Working on "$each" ..."
  convert -resize 440 "$each" "$each" >> /dev/null
  composite -gravity northeast -dissolve 15.3 $WATERMARK "$each" "$each" >> /dev/null
  echo "... Done!"
exit 0

Additional Options

Please check out the ImageMagick website for more information on the many options and features their products have to offer.

Console Framebuffer in Ubuntu

Posted by admin on December 25, 2007 under Tech Tips | 8 Comments to Read

The Linux console framebuffer allows you to achieve higher screen resolutions within your Linux console. However, as of this writing, Ubuntu 7.10 Gutsy by default does not load the required kernel modules. By passing the “vga=XXX” kernel option without these modules loaded, you are left with a blinking cursor in the upper-left hand corner of your screen. Here’s how to get the console framebuffer in working order.

1. Ensure the initrd image includes framebuffer support by adding “fbcon” and “vesafb” to /etc/initramfs-tools/modules.

$ echo "vesafb" | sudo tee -a /etc/initramfs-tools/modules
$ echo "fbcon" | sudo tee -a /etc/initramfs-tools/modules

2. Remove (or comment out) “vesafb” from blacklisted modules in /etc/modprobe.d/blacklist-framebuffer.

#blacklist vesafb

3. Add the desired framebuffer variable to the default kernel options section in /boot/grub/menu.lst. For 1024×768, the string should look like the following.

#defoptions=quiet splash vga=791

4. Update GRUB.

$ sudo update-grub

5. If Usplash is configured for a higher resolution than your framebuffer, it will appear off-centered. So adjust /etc/usplash.conf to use the same resolution.


6. Update initramfs to rebuild the initrd image.

$ sudo update-initramfs -u

After rebooting, your usplash will appear as normal and you can Ctrl+Alt+F1 to a console after your X environment has finished loading. The text in your console should now appear much smaller and will be much easier to use for large amounts of console work.

Batch MP3 Encoding with Linux and LAME

Posted by admin on December 2, 2007 under Tech Tips | 25 Comments to Read

If you have a number of audio files that you would like to convert to mp3, but don’t want to hassle with graphical applications, there is a simple way to accomplish the task using Linux, LAME and a little shell scripting. By performing a basic “for loop” to invoke LAME, you can easily convert any group of audio files using one line of shell code.

Here is an example of a “for loop” that runs the command “lame” against a set of files in your current working directory with the .wav file extension.

$ for f in *.wav ; do lame $f ; done

If your audio file names have spaces in them, then you will need to use quotation marks around “$f” variable.

$ for f in *.wav ; do lame "$f" ; done

I typically create my original audio files without the file name extension of .wav or .au. This is because when you run lame against a file name, and do not omit the extension in the output option, the resulting file will have two extensions in the file name. e.g. filename.wav.mp3. Yes, I can use sed or basename in the for loop to prevent this, but to keep it simple, I just choose to not use the file extension to begin with.

If you are working with a group of files that have all been named using the convention of “Artist – Album – ## – Track Title”, (notice the spaces in the name), the following will work.

$ for f in Artist - Album* ; do lame "$f" ; done

Once the job is finished, you will be left with a directory full of your original audio files, and your newly created mp3’s.

Extra Credit

Okay, since we’re on the topic of shell scripting. If you want to delete all the original audio files (the ones without any file name extensions), and without first moving the new mp3’s to a different directory, one overly complicated example would be the following.

for f in *.mp3 ; do AUFILE=`basename "$f" .mp3` ; rm "$AUFILE"; done

This would have been easier if the original files could have been identified with .wav extensions, (rm *.wav), but since they had no file extensions to begin with, a wild card alone would not work. Now folks, this is just an example, and there’s a million other ways you can go about this. But in any case, I hope it helps you start exploring on your own!