Automagic blending of wallpapers using RMagick
I wanted a program that let me blend random wallpapers from a directory together and set them every minute with increasing opacity on one image, and then to select a new random image and repeat the process. I did this using RMagick and some Ruby.
Make the following tree:
~/.wallmold/
current.yml
Fill current.yml with this:
---
file2: someimage
wallpaperdir: wallpaper directory
dissolution: 0.1
file1: anotherimage
Replacing the image names and directory with the proper stuffs, full path on directory and relative on image names. Here’s mine:
---
file2: w09.jpg
wallpaperdir: /home/hank/MyDocs/Wallpapers
dissolution: 0.8
file1: Looking_Forward.jpg
Then, get this, make it executable, and put it somewhere:
#!/usr/bin/env ruby
# Wallmold - a wallpaper melding script
require 'RMagick'
require 'yaml'
class Array
def randomize
arr=self.dup
arr.collect { arr.slice!(rand(arr.length)) }
end
def randomize!
arr=self.dup
result = arr.collect { arr.slice!(rand(arr.length)) }
self.replace result
end
end
# Load State
configpath = "#{ENV['HOME']}/.wallmold/current.yml"
configfile = File.open(configpath, 'r')
config = YAML.load(configfile)
out = "#{ENV['HOME']}/.wallmold/dissolve.jpg"
# Open the Wallpaper directory
dir = Dir.open(config['wallpaperdir'])
newconfig = config
if config['dissolution'] == 0.9
# Get new images
files = dir.to_a.randomize
newconfig['file1'] = config['file2']
newconfig['file2'] = files.pop
newconfig['dissolution'] = 0.1
else
newconfig['file1'] = config['file1']
newconfig['file2'] = config['file2']
newconfig['dissolution'] = config['dissolution'] + 0.1
end
bgnd = Magick::Image.read(dir.path+"/"+newconfig['file1']).first
overlay = Magick::Image.read(dir.path+"/"+newconfig['file2']).first
# Make the first image is the same size as the second
bgnd.crop_resized!(overlay.columns, overlay.rows)
composited = bgnd.dissolve(overlay, newconfig['dissolution'])
composited.write(out)
`fbsetbg #{out}`
# Write new config
configfile.close
configfile = File.open(configpath, 'w')
configfile.puts newconfig.to_yaml
Now, just edit your crontab:
* * * * * DISPLAY=:0 ruby -r rubygems /home/hank/bin/wallmold.rb > /dev/null 2>&1
