background magic

19 09 2011

Back again. So you got some important date/deadline and want to display it permantly on your desktop wallpaper?

wallpaper with additional text

Wallpaper with deadline text

One solution comes with imagemagick. It provides a way to directly push text into an image file. This text can be overlayed on an existing image file and afterwards set this resulting image as wallpaper (for GNOME environment in the following script).

The way the following script works? Ok, first we copy the desired image to a predefined location. Then we resize it to the screen width/heigth (depending on the source image aspect ratio) and crop its borders. Then we overlay the desired boxes and text. Finally we (re)set it als the gnome wallpaper.
If you just execute the script it will only update the days/hours, if you execute it with a variable, it will use the given image as the new background image.

# imagemagick and exiftool required
# maybe someone wants to replace the exiftool-part with identify (also part of imagemagick)

## initial conditions
# empty source file without text overlay
if [[ $1 != "" ]]	# if new file is chosen
        then    cp $1 $sourcefile	# replace old file
fi			# sourcefile necessary for hourly updates

# fonts used (best used with complete paths)

# opacity of overlay text/lines

# hours (stunden), days (tage)
stunden=$(( ($(date -d "2011-09-22 11:23" +%s) - $(date +%s))/(60*60) ))
tage=$(( $stunden / 24 ))

## image analysis, background resizing and cropping
# monitor/background resolution
# image resolution
sourcewidth=$(exiftool -ImageWidth $sourcefile | sed "s/.*: //g")
sourceheight=$(exiftool -ImageHeight $sourcefile | sed "s/.*: //g")
# calculation of resize-factor
# something a bit more complicated to calculate the image aspect ratio because I found no way to calculate float numbers in the bash. so I just use very big numbers...
afactor=$(( $sourcewidth * $height ))
bfactor=$(( $sourceheight * $width ))
# source-file - portrait or landscape-mode?
if [[ $afactor > $bfactor  ]]
then resizefactor=x$height
	echo lanscape
else resizefactor=$width
	echo portrait
# resize and crop source image
convert $sourcefile -resize $resizefactor \
                    -gravity South -crop ${width}x${height}+0+0 $targetfile

## create and overlay text and boxes
# in detail: 
# - draw 2 boxes with round corners
# - place day/hour variables
# - place day/hour text
# for more info...see imagemagick/convert man page or homepage
convert $targetfile \
	-fill transparent -stroke black -strokewidth 5 -gravity center \
       	-draw "stroke-opacity $opacity roundrectangle 1040,70 1240,270 20,20 \
	       roundrectangle 1040,315 1240,475 20,20" \
	-strokewidth 1 -fill black -stroke transparent \
	-font $font1 -pointsize 130 -draw "fill-opacity $opacity text 499,-260 '$tage' " \
	             -pointsize 100 -draw "fill-opacity $opacity text 499,-30 '$stunden' " \
	-font $font2 -pointsize 40 -draw "fill-opacity $opacity text 499,-157 'Tage' " \
	             -pointsize 28 -draw "fill-opacity $opacity text 499,47 'Stunden' " \

## set image as background (gnome only)
gconftool-2 -t str --set /desktop/gnome/background/picture_filename $baum$1
gconftool-2 -t str --set /desktop/gnome/background/picture_options stretched
# alternatives: "none", "wallpaper", "centered", "scaled", "stretched", "zoom", "spanned"

So…if you like to…just adapt it to your needs, display the joke of the day or whatever you like.


compress images in pdf files

30 06 2011

so this might be useful if you want to forward/store a pdf file (newsletter etc.) that contains uncompressed images. Using ghostscript you can resize them inside the pdf file.
$ pdfcompress inputfile.pdf outputfile.pdf

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=$2 $1
# more options:

With thanks to Milan Kupcevic for the inspiration. More options (especially regarding the image resolution) can be found on his page.