1 简介

在电脑桌面设置一组可以定时更换的壁纸已经不是什么新奇的功能了。比如,Windows 7、KDE桌面环境都可以实现这样的效果。可是,自己目前使用的Debian系统并未安装KDE、GNOME这样的桌面环境,只是简单的X视窗系统与Sawfish窗口管理。因此,要想实现加载壁纸并定时更换的功能就需要另想办法了。经过一番思考与编程,我现在可以用如下一系列脚本程序来完成这个任务:

  1. get_img_size.sh :调用 identify 命令显示图片信息并使用 gawk 命令从中提取出图片的尺寸。
  2. set_wallpaper.sh :调用 xloadimage 程序,加载单张图片作为壁纸。其中,该脚本会调用 get_img_size.sh 以获取该图片的尺寸,并根据指定的屏幕大小与其所在位置,对该图片进行适当的缩放与平移,使其显示于屏幕中间,且保持原比例充满屏幕。
  3. set_multi_wallpapers.sh :读入包含壁纸图片列表的配置文件,以指定的时间间隔来调用 set_wallpaper.sh ,从而逐个将这些图片加载为壁纸。
  4. gen_multi_wallpapers.sh :根据指定的匹配模式,使用 find 命令列出匹配到的图片文件,并生成能被 set_multi_wallpapers.sh 使用的配置文件。

下面对上述脚本进行具体说明。

2 加载单张图片作为壁纸: set_wallpaper.sh

将图片作为壁纸显示是通过 xloadimage 命令来实现的。通常,可以按如下的方式使用它:

  1. xloadimage -fullscreen -onroot -border black -zoom 100 -rotate 90 -at 0,0 image_file

其中, -fullscreen-onroot 的使用就可以将图片作为壁纸显示而不是被放到一个窗口里。其余的命令行选项的含义都是不言自明的。 set_wallpaper.sh 脚本程序并不是简单地调用 xloadimage ,其还会根据图片的大小与屏幕尺寸计算出 -zoom 选项的值,从而自动缩放图片,使其恰好完整地填满屏幕。此外,若电脑连接有多个显示器,可以通过设置 set_wallpaper.sh-x-y 选项来指定待加载壁纸屏幕左上角的坐标,以及设置 -W-H 选项来指定该屏幕的宽和高。最终,图片就会只在该屏幕上显示了。

set_wallpaper.sh 的源码如下:

  1. #!/bin/bash
  2.  
  3. script_name="set_wallpaper.sh"
  4. script_usage=$(cat <<EOF
  5. $script_name [OPTION] image_file
  6. EOF
  7. )
  8. script_function=$(cat <<EOF
  9. This script is used to load an image onto root screen as wallpaper.
  10. EOF
  11. )
  12. script_doc=$(cat <<EOF
  13. -h Display this help.
  14. -r Set the rotation angle of the image.
  15. -W Screen width.
  16. -H Screen height.
  17. -x X position of the image to be loaded onto root screen.
  18. -y Y position of the image to be loaded onto root screen.
  19. EOF
  20. )
  21. script_examples=$(cat <<EOF
  22. $script_name -r 0 -W 1920 -H 1080 -x 1280 -y 0 $picture/wallpapers/sunflower.jpg
  23. EOF
  24. )
  25. state_prefix="==="
  26. warning_prefix="***"
  27. error_prefix="!!!"
  28.  
  29. function display_help() {
  30. if [ -n "$script_usage" ]; then
  31. echo -e "Usage: $script_usage"
  32. fi
  33.  
  34. if [ -n "$script_function" ]; then
  35. echo -e "$script_function"
  36. fi
  37.  
  38. if [ -n "$script_doc" ] ; then
  39. echo -e "\n$script_doc"
  40. fi
  41.  
  42. if [ -n "$script_examples" ]; then
  43. echo -e "\nExamples"
  44. echo -e "$script_examples"
  45. fi
  46. }
  47.  
  48. # Default parameters
  49. declare -i rot_angle=0 screen_width=1920 screen_height=1080 screen_x=0 screen_y=0 zoom_factor=100 x_res=0 y_res=0 x_res_scaled=0 y_res_scaled=0 at_x=0 at_y=0
  50.  
  51. while getopts ":r:W:H:x:y:h" opt; do
  52. case $opt in
  53. r ) rot_angle=$OPTARG
  54. if [ $(((($rot_angle % 90) != 0) || (($rot_angle < 90) && ($rot_angle != 0)))) = 1 ]; then
  55. echo "The rotation angle of the image should be 0 or multiple of 90 degrees!"
  56. exit 1
  57. fi ;;
  58. W ) screen_width=$OPTARG ;;
  59. H ) screen_height=$OPTARG ;;
  60. x ) screen_x=$OPTARG ;;
  61. y ) screen_y=$OPTARG ;;
  62. h ) display_help
  63. exit 0 ;;
  64. \? ) display_help
  65. exit 1 ;;
  66. esac
  67. done
  68.  
  69. shift $(($OPTIND - 1))
  70.  
  71. if [ -n "$1" ]; then
  72. echo "Loading image $1"
  73. resolution=`get_img_size.sh "$1"`
  74. x_res=`echo $resolution | gawk -F x '{print $1}'`
  75. y_res=`echo $resolution | gawk -F x '{print $2}'`
  76.  
  77. # If the rotation angle is odd times of 90 degrees swap x and y
  78. # resolutions of the image
  79. if [ $((($rot_angle / 90 % 2) == 1)) = 1 ]; then
  80. tmp_res=$x_res
  81. x_res=$y_res
  82. y_res=$tmp_res
  83. fi
  84. echo "Image resolution is ${x_res}x${y_res}"
  85.  
  86. # Calculate the scaling factor of the image to fit to the screen
  87. if [ $((($x_res * 100 / $y_res) < ($screen_width * 100 / $screen_height))) = 1 ]; then
  88. # Scale the height of the image
  89. if [ $(($y_res > $screen_height)) = 1 ]; then
  90. zoom_factor=$(($screen_height * 100 / $y_res))
  91. else
  92. zoom_factor=$(($y_res * 100 / $screen_height))
  93. fi
  94. echo "Scaling in the y direction!"
  95. at_y=$screen_y
  96. at_x=$((($screen_width - $x_res * $zoom_factor / 100) / 2 + $screen_x))
  97. else
  98. # Scale the width of the image
  99. if [ $(($x_res > $screen_width)) = 1 ]; then
  100. zoom_factor=$(($screen_width * 100 / $x_res))
  101. else
  102. zoom_factor=$(($x_res * 100 / $screen_width))
  103. fi
  104. echo "Scaling in the x direction!"
  105. at_x=$screen_x
  106. at_y=$((($screen_height - $y_res * $zoom_factor / 100) / 2 + $screen_y))
  107. fi
  108. echo "Zoom factor is $zoom_factor"
  109. echo "Image location is $at_x,$at_y"
  110.  
  111. rot_opt=""
  112. if [ $(($rot_angle != 0)) = 1 ]; then
  113. rot_opt="-rotate $rot_angle"
  114. fi
  115. xloadimage -fullscreen -onroot -border black -zoom $zoom_factor $rot_opt -at $at_x,$at_y "$1"
  116. else
  117. echo "Please provide an image!"
  118. exit 1
  119. fi

其中,用于获取图片大小的脚本 get_img_size.sh 内容如下:

  1. #!/bin/bash
  2.  
  3. if [ -n "$1" ]; then
  4. identify "$1" | gawk '{if(match($0, /([[:digit:]]+)x([[:digit:]]+)/, res) != 0) print res[0];}'
  5. else
  6. echo "Please provide an image!"
  7. exit 1
  8. fi

3 定时加载多张壁纸: set_multi_wallpapers.sh

通过 -s 选项,可以指定配置文件,其由该脚本读入后以默认的时间间隔60秒或由 -t 选项指定的时间间隔来逐个显示列表中的图片。配置文件的格式如下:

  1. # Configuration file storing list of images to be loaded onto root screen as wallpapers.
  2.  
  3. # Description of data columns (columns are separated by TAB):
  4. # Column 1: Absolute path to image
  5. # Column 2: Rotation angle applied to the image
  6. # Column 3: X position of the image to be loaded onto the root screen
  7. # Column 4: Y position of the image to be loaded onto the root screen
  8.  
  9. /home/orlando/Pictures/wallpapers/Windows 10 wallpapers/02 - wqQXJGJ.jpg 0 1280 0
  10. /home/orlando/Pictures/wallpapers/Windows 10 wallpapers/03 - jVKcv41.jpg 0 1280 0
  11. /home/orlando/Pictures/wallpapers/Windows 10 wallpapers/04 - wPpANVz.jpg 0 1280 0
  12. /home/orlando/Pictures/wallpapers/Windows 10 wallpapers/05 - yGQyrcX.jpg 0 1280 0
  13. /home/orlando/Pictures/wallpapers/Windows 10 wallpapers/07 - XaanvJe.jpg 0 1280 0
  14. /home/orlando/Pictures/wallpapers/Windows 10 wallpapers/08 - zl3dIFY.jpg 0 1280 0
  15. /home/orlando/Pictures/wallpapers/Windows 10 wallpapers/09 - VD7t2al.jpg 0 1280 0
  16. /home/orlando/Pictures/wallpapers/Windows 10 wallpapers/10 - GjUEn90.jpg 0 1280 0
  17. /home/orlando/Pictures/wallpapers/Windows 10 wallpapers/11 - qX58uOK.jpg 0 1280 0
  18. /home/orlando/Pictures/wallpapers/Windows 10 wallpapers/20 - 1QqFwJU.jpg 0 1280 0
  19. /home/orlando/Pictures/wallpapers/Windows 10 wallpapers/Diver.jpg 0 1280 0

set_multi_wallpapers.sh 脚本内容如下:

  1. #!/bin/bash
  2.  
  3. script_name="set_multi_wallpapers.sh"
  4. script_usage=$(cat <<EOF
  5. $script_name [OPTION]
  6. EOF
  7. )
  8. script_function=$(cat <<EOF
  9. This script is used to display a set of images onto root screen as wallpapers.
  10. EOF
  11. )
  12. script_doc=$(cat <<EOF
  13. -h Display this help.
  14. -s Source file storing list of image paths and parameters. If this is not given, ~/.multi_wallpapers will be used.
  15. -t Time interval between each image. If this is not given, default interval is 60 seconds.
  16. -W Screen width.
  17. -H Screen height.
  18. EOF
  19. )
  20. script_examples=$(cat <<EOF
  21. $script_name # Load images in ~/.multi_wallpapers as wallpapers with an interval of 60 seconds.
  22. $script_name -s /path/to/image_list_file -t 5 # Specify image list file and time interval.
  23. EOF
  24. )
  25. state_prefix="==="
  26. warning_prefix="***"
  27. error_prefix="!!!"
  28.  
  29. function display_help() {
  30. if [ -n "$script_usage" ]; then
  31. echo -e "Usage: $script_usage"
  32. fi
  33.  
  34. if [ -n "$script_function" ]; then
  35. echo -e "$script_function"
  36. fi
  37.  
  38. if [ -n "$script_doc" ] ; then
  39. echo -e "\n$script_doc"
  40. fi
  41.  
  42. if [ -n "$script_examples" ]; then
  43. echo -e "\nExamples"
  44. echo -e "$script_examples"
  45. fi
  46. }
  47.  
  48. # Default parameters
  49. image_source_file="/home/orlando/.multi_wallpapers"
  50. time_interval=60
  51. screen_width=1920
  52. screen_height=1080
  53.  
  54. # Process command options
  55. while getopts ":ht:s:W:H:" opt; do
  56. case $opt in
  57. h ) display_help
  58. exit 0 ;;
  59. s ) image_source_file=$OPTARG ;;
  60. t ) time_interval=$OPTARG ;;
  61. W ) screen_width=$OPTARG ;;
  62. H ) screen_height=$OPTARG ;;
  63. \? ) display_help
  64. exit 1 ;;
  65. esac
  66. done
  67. shift $(($OPTIND - 1))
  68.  
  69. # Start execute the command
  70. IFS=$'\n'
  71. if [ -e "$image_source_file" ]; then
  72. while [ 1 ]; do
  73. for image_info in `cat "$image_source_file" | grep "^[^#].*"`; do
  74. image_name=`echo "$image_info" | cut -f 1`
  75. rot_angle=`echo "$image_info" | cut -f 2`
  76. x_pos=`echo "$image_info" | cut -f 3`
  77. y_pos=`echo "$image_info" | cut -f 4`
  78.  
  79. if [ -e "$image_name" ]; then
  80. set_wallpaper.sh -r "$rot_angle" -W "$screen_width" -H "$screen_height" -x "$x_pos" -y "$y_pos" "$image_name"
  81. else
  82. echo -e "$error_prefix Image $image_name does not exist!"
  83. fi
  84.  
  85. sleep $time_interval
  86. done
  87. done
  88. else
  89. echo -e "$error_prefix Configuration file $image_source_file does not exist!"
  90. fi

4 生成多张壁纸配置文件: gen_multi_wallpapers.sh

该脚本可以在指定的目录下(第一个参数),根据指定的正则表达式(第二个参数)来搜索出匹配的图片文件,然后将其按上述配置文件的格式显示出来。脚本内容如下:

  1. #!/bin/bash
  2.  
  3. script_name="gen_multi_wallpapers.sh"
  4. script_usage=$(cat <<EOF
  5. $script_name [OPTION] path_name regex_pattern
  6. EOF
  7. )
  8. script_function=$(cat <<EOF
  9. Generate configuration for a list of wallpaper images.
  10. EOF
  11. )
  12. script_doc=$(cat <<EOF
  13. -h Display this help.
  14. -c Display the comment for configuration file.
  15. -x X position of the image to be loaded onto root screen.
  16. -y Y position of the image to be loaded onto root screen.
  17. EOF
  18. )
  19. script_examples=$(cat <<EOF
  20. $script_name -x 1280 ./ "\\(.*jpg\\)\\|\\(.*JPG\\)"
  21. EOF
  22. )
  23. state_prefix="==="
  24. warning_prefix="***"
  25. error_prefix="!!!"
  26.  
  27. function display_help() {
  28. if [ -n "$script_usage" ]; then
  29. echo -e "Usage: $script_usage"
  30. fi
  31.  
  32. if [ -n "$script_function" ]; then
  33. echo -e "$script_function"
  34. fi
  35.  
  36. if [ -n "$script_doc" ] ; then
  37. echo -e "\n$script_doc"
  38. fi
  39.  
  40. if [ -n "$script_examples" ]; then
  41. echo -e "\nExamples"
  42. echo -e "$script_examples"
  43. fi
  44. }
  45.  
  46. # Default parameters
  47. x_pos=0
  48. y_pos=0
  49. comment_string=$(cat <<EOF
  50. # Configuration file storing list of images to be loaded onto root screen as wallpapers.
  51.  
  52. # Description of data columns (columns are separated by TAB):
  53. # Column 1: Absolute path to image
  54. # Column 2: Rotation angle applied to the image
  55. # Column 3: X position of the image to be loaded onto the root screen
  56. # Column 4: Y position of the image to be loaded onto the root screen
  57. EOF
  58. )
  59. is_display_comment=0
  60.  
  61. # Process command options
  62. while getopts ":chx:y:" opt; do
  63. case $opt in
  64. x ) x_pos=$OPTARG ;;
  65. y ) y_pos=$OPTARG ;;
  66. c ) is_display_comment=1 ;;
  67. h ) display_help
  68. exit 0 ;;
  69. \? ) display_help
  70. exit 1 ;;
  71. esac
  72. done
  73. shift $(($OPTIND - 1))
  74.  
  75. # Start execute the command
  76. if [ -n "$1" ] && [ -n "$2" ]; then
  77. if [ "$is_display_comment" = 1 ]; then
  78. echo -e "$comment_string\n"
  79. fi
  80.  
  81. gawk_cmd=`echo "gawk '{print \\$0\"\\t0\\t$x_pos\\t$y_pos\"}'"`
  82. find "$1" -regex "$2" -print0 | xargs -n 1 -0 fullpath.sh | eval "$gawk_cmd"
  83. else
  84. echo "$error_prefix Please specify search path name and file name pattern!"
  85. fi

可以看出,该脚本又用到了 fullpath.sh ,用于显示图片文件的绝对路径。 fullpath.sh 的内容如下:

  1. #!/bin/bash
  2.  
  3. echo $(cd $(dirname "$1") && pwd -P)/$(basename "$1")

5 脚本的使用

至此,一切都准备就续,可以按下面的步骤来实战了:

首先,切换到包含自己喜欢图片的文件夹,执行下面的命令生成配置文件:

  1. gen_multi_wallpapers.sh -c -x 1280 ./ "\\(.*jpg\\)\\|\\(.*JPG\\)" > ./.multi_wallpapers

接下来,将下面的命令加入到 ~/.xinitrc 中:

  1. set_multi_wallpapers.sh -s ./.multi_wallpapers -t 300 &

最后,重启X server即可生效。截了一个视频在这里(不好意思,用的是微软最新发布的Windows 10壁纸)。

在GNU/Linux下设置与定时更换桌面壁纸的更多相关文章

  1. 我在GNU/Linux下使用的桌面环境工具组合

    为了使GNU/Linux桌面环境下加载的程序较少以节省内存资源和提高启动时间,我目前并不使用重量级的桌面环境KDE和Gnome,甚至连登录窗界面gdm或xdm都不用,而是直接启动到控制台,登录后调用s ...

  2. linux下使用crontab定时备份MYSQL数据库的方法:

    摘要 linux下使用crontab定时备份MYSQL数据库的方法: 只需按照下面3步做,一切都在你的掌控之下: 第一步:在服务器上配置备份目录代码: ------------------------ ...

  3. GNU/Linux下Freeplane的界面渲染问题

    如下图所示,思维导图软件Freeplane在GNU/Linux下默认的界面渲染效果是很差的,即便将Preferences → Appearance → Antialias设置为Antialias al ...

  4. 在GNU/Linux下将CD音乐转为mp3

    以前我欣赏古典音乐都是听的CD,因而珍藏了不少光盘以及下载到电脑上的ape与flac格式的音乐文件.随着手机硬件性能(如电池续航能力.处理器速度.音质.存储容量等)和软件功能(音乐播放器对于曲目的管理 ...

  5. Linux下设置网卡静态ip

    Linux下设置网卡静态ip 如果是服务器版,没有图形界面只用用命令行修改配置文件 如果是客户端版本,可以用图形界面 配置的前提是要在root用户下才能重启网卡服务 图形界面: system-conf ...

  6. Ubuntu Linux下设置IP的配置命令

    Ubuntu Linux下设置IP的配置命令 今天装了Ubuntu,但是发现不能上网,开始排查问题: 1.首先确定网络连接是否正确,所用的网线是否可以正常工作 2.查看网卡是否能正常工作,检测的方法如 ...

  7. Linux下设置时间

    Linux下设置时间 提供两种最根本有效的方式,就是更改时区.这里以更改为国内上海时间例子,其他地方时区同理. 方法一 备份文件 mv /etc/localtime /etc/localtime.ba ...

  8. linux下设置计划任务执行python脚本

    linux下设置计划任务执行python脚本 简介 crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自 ...

  9. CentOS/Linux下设置IP地址

    CentOS/Linux下设置IP地址 1:临时修改:1.1:修改IP地址# ifconfig eth0 192.168.100.100 1.2:修改网关地址# route add default g ...

随机推荐

  1. HDU--杭电--3415--Max Sum of Max-K-sub-sequence--暴力或单调队列

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  2. 简单的mvc之二:蜿蜒的管线

    关于系列的第二篇,在管线与路由之间犹豫了很久,最终选择了管线—为免于盲人摸象的困惑. 管线的位置在哪里呢?webform,mvc以及web api都架构于asp.net平台上,管线则是asp.net的 ...

  3. Linux 中 crontab 详解及示例

    cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动.关闭这个服务: /sbin/service c ...

  4. node.js系列笔记之node.js初识《一》

    node.js系列笔记之node.js初识<一> 一:环境说明 1.1 Linux系统CentOS 5.8 1.2 nodejs v0.10.15 1.3 nodejs源码下载地址 htt ...

  5. go实现排序的链表

    go实现排序的链表 Posted on 2013-08-14 23:25 mac的学习笔记 阅读(224) 评论(0) 编辑 收藏 链表的数据结构比较线性数组,优点是 可以方便的对任意的位置进行插入和 ...

  6. LESS编译方案

    我的LESS编译方案 2013-08-07 10:22 by 逆风之羽, 469 阅读, 2 评论, 收藏, 编辑 背景 近期项目前端决定使用less,简单介绍一下,详细信息有兴趣查看官方文档(htt ...

  7. 字符转十六进制 String => HEX using "hexdump" on linux

    hexdump 是一款非常简单的有效的将别的形式的文档转换成十六进制的工具. 最详细的使用说明都在 man hexdump 里面,请自行查阅. 这里我摘出几个常用的例子,一目了然: 这里我做了一个实验 ...

  8. LINUX安装SVN+添加自动同步+远程下载最新代码

    LINUX安装SVN+添加自动同步+远程下载最新代码---------------------1. 新建一个用户:svnroot ,以下操作非特别说明皆为root用户操作--------------- ...

  9. linux命令——iotop

    查看CPU使用情况用top,查看I/O使用情况就需要iotop.这个命令是在 kernel v2.6.20中添加,安装的时候要注意内核的版本号. iotop常用快捷键 1. 左右箭头 --> 改 ...

  10. JBPM之JPdl小叙

    JPdl:jbpm流程定义文件,下面是一个简单的流程定义文件: <?xml version="1.0" encoding="UTF-8"?> < ...