The delete key isn't enough. I want a computer that flushes.

Script of the Day: Random Wallpaper Setter

Date/Time Permalink: 10/08/12 02:44:56 pm
Category: HOWTOs and Guides

Y'know, I get tired sometimes of dealing with all the different screens. Between several cell phones, a laptop, and several desktop PCs, I have about eight different devices that want their default screen space filled with something and eight different methods to use for filling them. Bah, I said, and also there's still no default mechanism in my favorite desktop, Fluxbox, for setting the wallpaper from the menu. Furthermore, for those times when an image doesn't scale easily onto screen dimensions, there's also no easy way to set the surrounding fill color.

This is, Which I load into my menu system and launch with one click. It picks a random file out of my "graphics/Walls" directory, slaps it onto a full-screen background (automatically optimized for whatever screen it's running on) of a neutral color picked from the image itself (composing a new image in the process), and slaps it onto the screen. And then I get on with my life...


# Step 1 - grab random file
FILE=$(ls ~/graphics/Walls/ | shuf -n1)
convert ~/graphics/Walls/$FILE ~/.temp.png

# Step 2 - Determine fill color
FILLCOLOR=$(convert ~/.temp.png -scale 1x1\! -format '%[pixel:u]' info:-)

# Step 3 - Determine the geometry of the root window
GEOM=$(xwininfo -root | grep -A 1 Width)
GEOMX=$(echo $GEOM | awk '{print $2}')
GEOMY=$(echo $GEOM | awk '{print $4}')

# Step 4 - Make a new background image with the fill color and root window dimensions
convert -size $SIZE xc:$FILLCOLOR ~/.current_wallpaper.png
convert ~/.temp.png -resize $SIZE\> ~/.temp.png

# Step 5 - Combine wallpaper and background and display
composite -gravity center ~/.temp.png ~/.current_wallpaper.png ~/.current_wallpaper.png
rm ~/.temp.png
display -window root ~/.current_wallpaper.png

exit 0

Ugly as grandma Moses, isn't it? I expect the usual commenters pointing out that I do everything the hard way.

What we're doing in step 2, is we're taking the wallpaper image and making a new temporary file where we scale that wallpaper down to a single pixel - this will produce the average color from the image. Then use Image Magick's 'info' feature to read the RGB of that one-pixel image, and that becomes the background color. The rest is just a few 'combine' statements and use of the little-seen GNU 'shuf' command, handy for whenever you want a random file from a directory.

As long as a system has X11, Image Magick, and GNU standard utilities, it should work. Feel free to snarf bits and bobs for your own uses.

Follow me on Twitter for an update every time this blog gets a post.
Stumble it Reddit this share on Facebook

suddenly the moon