Skip to content

Instantly share code, notes, and snippets.

@penguin2716
Created February 9, 2014 07:01
Show Gist options
  • Save penguin2716/8895427 to your computer and use it in GitHub Desktop.
Save penguin2716/8895427 to your computer and use it in GitHub Desktop.
update wallpaper after scaling and cropping the image to the current screen resolution
#!/bin/bash
#
# asetroot.sh
#
# Copyright (c) 2014 Takuma Nakajima
#
# This software is released under the MIT License.
# http://opensource.org/licenses/mit-license.php
#
set -e
function autocrop() {
srcimg=$1
target_x=`echo $2 | awk -F 'x' '{print $1}'`
target_y=`echo $2 | awk -F 'x' '{print $2}'`
outimg=$3
tmpimg=/dev/shm/tmp_mkwallpaper.jpg
src_x=`identify $srcimg | awk '{print $3}' | awk -F 'x' '{print $1}'`
src_y=`identify $srcimg | awk '{print $3}' | awk -F 'x' '{print $2}'`
if [ `expr ${src_x} \* ${target_y} / ${src_y}` -gt $target_x ]; then
tmp_x=`expr ${src_x} \* ${target_y} / ${src_y}`
convert -geometry ${tmp_x}x${target_y}! $srcimg $tmpimg
convert -crop ${target_x}x${target_y}+`perl -e "print abs( $tmp_x - $target_x ) / 2"`+0 $tmpimg $outimg
else
tmp_y=`expr ${src_y} \* ${target_x} / ${src_x}`
convert -geometry ${target_x}x${tmp_y}! $srcimg $tmpimg
convert -crop ${target_x}x${target_y}+0+`perl -e "print abs( $tmp_y - $target_y ) / 2"` $tmpimg $outimg
fi
rm -f $tmpimg
}
wallpaper=$1
if [ $# -ne 1 ]; then
echo "usage: $0 <image_file>"
exit 1
elif [ ! -e $wallpaper ]; then
echo "error: image not found: $wallpaper"
exit 1
fi
output=/dev/shm/wallpaper.png
current_resolution=`xrandr -q | grep -Eo 'current[^,]+' | sed 's/\(current\)\? //g'`
echo "resizing image to ${current_resolution}..."
autocrop $wallpaper $current_resolution $output
echo "updating wallpaper..."
hsetroot -fill $output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment