点此下载 (包含脚本及图标和壁纸素材)
看LinuxToy时发现了一个天气墙纸的程序http://linuxtoy.org/archives/weather-wallpaper.html,貌似依赖不少的Gnome组件,很不方便,于是就萌生了用bash脚本写一个相同功能的脚本的想法,经过几个小时的奋战终于搞定了,呵呵,主要是convert等处理图片的软件用得不熟。
这个东东可以自动去获取天气信息(你甚至都不用管城市代码,当然可能有些地方会不准),然后根据获得的天气找到对应的图标,然后将图标和天气情况的文字(是否绘制文字信息可选)合成到背景图片中去,然后将这个合成的图片设置为壁纸。
初次使用需要配置一些东西,主要就是字体,因为我用的雅黑你可能没有。
这里有一张生成的图片大家可以感受一下: 代码如下:
代码: 全选
#!/bin/bash
#Copyright (c) 2009 xiooli (xioooli[at]yahoo.com.cn, http://joolix.com)
#Name wallther
#License: GPLv3
#Version 20090511
#此脚本需要安装 w3m 和 imagemagick
#城市代码,留空可自动检测(自动检测不一定精确)
#城市代码可在 http://weather.265.com 上查询,是个5位的数字
#受bones7456和wenbob的天气脚本启发。
#这儿是城市代码,须自己改,未赋值会自动查询。
Wid=101280101
#天气图标的位置
pwd="`dirname $0`"
if [ "$pwd" = "." ]; then pwd="`pwd`"; fi
Icondir="$pwd/icons"
#欲用作背景的图片
BackPic="$Icondir/background.jpg"
#最终输出的图片位置
OutPic="/dev/shm/wallpapertmp.png"
#壁纸路径(此为 OutPic 的副本)
Wallpaper="/dev/shm/wallpaper.png"
#是否在图片上绘制文字天气信息,yes/no
DrawText="yes"
#文字的字体,若不是中文字体则中文可能无法正常显示
#Font="/usr/share/fonts/winfonts/msyh.ttf"
Font="/home/leros/.fonts/sthei.ttf"
#文字的大小
FontSize=24
#文字的颜色
TxtColor="white"
#文字信息绘制的位置
TxtPosX=700
TxtPosY=350
#隔多大距离绘制下一行(此距离包括本行的宽度)
TxtYIncr=35
#天气图标绘制的位置
PicGeometry="+650+80"
#壁纸更换的时间间隔(默认 30 分钟)
ChangeTime="30m"
WeatherCN=("晴" "多云" "阴" "雨" "雷阵雨" "雾" "雪" "雨夹雪")
WeatherEN=("sun" "suncloud" "cloud" "rain" "storm" "fog" "snow" "snowrain")
GET_WEATHER() {
[ -z "${Wid}" ] &&
if [ -f "/dev/shm/city" ] ;then
Wid="$(cat /dev/shm/city)"
else
Wid="`wget -q -O - 'http://www.265.com/lookupcity'|awk -F "'" '{print $2}'`"
echo ${Wid}>/dev/shm/city
fi
[ ! -z "${Wid}" ] && WeatherTxt="`w3m -dump "http://wap.weather.com.cn/wap/${Wid}/h24.shtml"`"
}
GEN_DRAW_TEXT() {
[ -z "${WeatherTxt}" ] && GET_WEATHER
if [ -z "${WeatherTxt}" ]; then
echo "未能获取天气 :("
else
echo "${WeatherTxt}"|sed -n "5p"|sed 's/ .*$//'
echo "${WeatherTxt}"|sed -n "6,10p"
fi \
|sed "s/^.*$/-draw \'text POSITION \"&\"\'/" \
|while read line; do
echo "$line"|sed "s/POSITION/$TxtPosX,$TxtPosY/"
((TxtPosY+=$TxtYIncr))
done|tr "n" " "
}
GEN_WEATHER_ICON() {
local tmp weathercn index
[ -z "${WeatherTxt}" ] && GET_WEATHER
[ -z "${WeatherTxt}" ] || tmp="`echo "${WeatherTxt}"|sed -n "6p"`"
j=0; k=0
for i in ${WeatherCN[@]}; do
[ "${tmp//$i}" != "$tmp" ] && weathercn[$j]="$i" && index[$j]="$k" && ((j++))
((k++))
done
[ "${#weathercn[@]}" -eq 0 ] && Weather[0]="unknown"
[ "${#weathercn[@]}" -eq 1 ] && Weather[0]="${WeatherEN[${index[0]}]}"
[ "${#weathercn[@]}" -eq 2 ] &&
if [ "`echo $tmp|grep "${weathercn[0]}转"`" ];then
Weather[0]="${WeatherEN[${index[0]}]}"
Weather[1]="${WeatherEN[${index[1]}]}"
else
Weather[0]="${WeatherEN[${index[1]}]}"
Weather[1]="${WeatherEN[${index[0]}]}"
fi
[ "${#weathercn[@]}" -eq 3 ] &&
{
Weather[0]="${WeatherEN[${index[0]}]}"
Weather[1]="${WeatherEN[${index[2]}]}"
}
if [ "${#Weather[@]}" -ge 2 ]; then
convert +append "$Icondir/${Weather[0]}.png" "$Icondir/${Weather[1]}.png" /dev/shm/weathericon.png
else
ln -sf "$Icondir/${Weather[0]}.png" /dev/shm/weathericon.png
fi
}
GEN_WALLPAPWE() {
GEN_WEATHER_ICON
[ -f $BackPic ] || BackPic="$Icondir/background.jpg"
if [ "$DrawText" = "yes" ]; then
draw="convert -font \"$Font\" -fill $TxtColor -pointsize $FontSize `GEN_DRAW_TEXT` \"$BackPic\" \"/dev/shm/backpictmp.png\""
eval $draw
composite -geometry "$PicGeometry" /dev/shm/weathericon.png /dev/shm/backpictmp.png "$OutPic"
else
composite -geometry "$PicGeometry" /dev/shm/weathericon.png "$BackPic" "$OutPic"
fi
}
while :; do
GET_WEATHER
[ "${WeatherTxt}" != "$tmp" ] && GEN_WALLPAPWE && tmp="${WeatherTxt}"
if [ -f "$OutPic" ];then
mv "$OutPic" "$Wallpaper"
gconftool-2 -s /desktop/gnome/background/picture_filename --type=string "$Wallpaper"
fi
sleep "$ChangeTime"
done