# HOWTO: Set background depending on time, weather, season...

## SerfurJ

EDIT:  see the project webpage for latest updates.

The following howto shows you how to set your background depending on the time of day and the weather/season.  Each day, you'll see a different image in the morning, mid-day, afternoon, sunset, and evening.  The images will also reflect the current weather or season depending on the configuration.  The images for each time of day are chosen randomly, so no two days will ever have the same set of images.  

A typical day might look something like this:Morning

Mid-day

Afternoon or Winter afternoon

Sunset

Evening

It's the second best thing to actually being outside!

Download some images to get your collection started, and unpack them somewhere on your harddrive.  (Here is my current collection.  Click on "free" at the bottom of the page.)  You'll also need to find some photos for winter and rainy days.

Put this in ~/bin/onewithnature

```
#!/usr/bin/python

import pymetar, sys, re, os, time

## configuration section

# path to wallpapers

wallpaperdir = "/path/to/wallpapers/"

# the closest weather station

station = "KGLS"

# background setter (and display)

bgsetter = "DISPLAY=:0.0 /usr/bin/fbsetbg -r "

## end configuration section

rf = pymetar.ReportFetcher(station)

rep = rf.FetchReport()

rp = pymetar.ReportParser()

pr = rp.ParseReport(rep)

pattern = re.compile('.*rain.*')

rainy = pattern.search(pr.getWeather())

temp = pr.getTemperatureFahrenheit()

if rainy:

    os.popen(bgsetter + wallpaperdir + 'rainy &>/dev/null')

elif temp < 40:

    os.popen(bgsetter + wallpaperdir + 'winter &>/dev/null')

else:

    os.popen(bgsetter + wallpaperdir + sys.argv[1] + ' &>/dev/null')

```

Edit the configuration section:  

Change the path to match where your image collection is.  

Set your weather station.

Set the background setter.  Since I use Fluxbox, I use fbsetbg to set the background, but you can use any background setter that supports random loading of images.

Make ~/bin/onewithnature is executable:

```
chmod 755 ~/bin/onewithnature
```

Install pymetar.

```
$ emerge pymetar
```

At the prompt, type:

```
$ crontab -e
```

and put the following in your crontab:

```
# CRONTAB for drei

#

#       1)                                              => Minute

#               2)                                      => Hour

#                       3)                              => Day

#                               4)                      => Month

#                                       5)              => Day of week

#                                               6)      => Command

#

#       1)      2)      3)      4)      5)      6)

#       |       |       |       |       |       |

## Different backgrounds to set mood depending on time of day

        0       5       *       *       *       /home/user/bin/onewithnature morning

        0       11      *       *       *       /home/user/bin/onewithnature mid-day

        0       13      *       *       *       /home/user/bin/onewithnature afternoon

        0       19      *       *       *       /home/user/bin/onewithnature sunset

        0       20      *       *       *       /home/user/bin/onewithnature evening

```

Adjust the sunset/evening times depending on your location.

That's it!

If you come across photos that fit in any of the categories, please share them.  Also, if you can think of any other categories or schemes, post them here.Last edited by SerfurJ on Fri May 13, 2005 8:00 pm; edited 6 times in total

----------

## alkan

here is what i run from cron every 30 minutes to set my kde and fluxbox wall paper randomly.

```

DCOPSERVER=`cat /home/user/.DCOPserver_c1_:0 | grep local`

export DCOPSERVER

find /usr/share/pixmaps/ -type f -iname "*.jpg" -o -iname "*.png" > /tmp/wallpapers

NB_LINES=$(expr $(wc -l /tmp/wallpapers | sed -e 's/ *//' | cut -f1 -d " "))

NB_RAND=0

while [ "$NB_RAND" -eq 0 ]

do

NB_RAND=$(expr $RANDOM \% $NB_LINES)

done

WP=`sed -n "${NB_RAND}p;${NB_RAND}q" /tmp/wallpapers`

#set fluxbox wallpaper

DISPLAY=:0.0 /usr/bin/fbsetbg -A $WP

#"#d6d7e7"'

#set kde wallpaper

/usr/kde/3.4/bin/dcop --user user --all-sessions kdesktop KBackgroundIface setWallpaper $WP 4

#/usr/kde/3.4/bin/dcop --user user --all-sessions kdesktop-screen-1 KBackgroundIface setWallpaper $WP 4

```

by the way, another script downloads a bunch of wall papers every week from a website. I'd rather not give that scrip since it is not really ......

----------

## SerfurJ

thanks.. but this howto isn't really about setting a random wallpaper.  it's more about how to set the mood of your desktop depending on the time of day.

if you have relevant images or other ideas, please share.

----------

## SerfurJ

i just rewrote the howto.  it was previously "HOWTO: Be One with Nature using cron".  it should be more thorough and user friendly now.  

be sure to post feedback if you try it out.

----------

## Digital Storm

Very cool, I'll have to set it up once I'm back to using Gentoo...

Although it would be cool to find a nice location and set up a camera to snap photos every 30 mins...Then have your background change with the time of the day...It would obviously take time to find a nice spot and take the photos but it would be cool...

----------

## SerfurJ

 *Digital Storm wrote:*   

> Very cool, I'll have to set it up once I'm back to using Gentoo...

 

thanks.  it's not dependent on gentoo, but you need a linux/unix system.

 *Digital Storm wrote:*   

> Although it would be cool to find a nice location and set up a camera to snap photos every 30 mins...Then have your background change with the time of the day...It would obviously take time to find a nice spot and take the photos but it would be cool...

 

yeah, i thought about taking photos at a scenic location at 7am, 11am, 3pm, 7pm, and 9pm.  it would be some work though.  i'm about to take a trip to a beautiful area around seattle, washington, so i'll try this out if i have time.

----------

## SerfurJ

i've set up a project webpage at:

http://www.dunsm.org/projects/one-with-nature.html

----------

## pjp

Cool idea.  I've been wanting to do something very similar, only with moon phases.

----------

## SerfurJ

 *Quote:*   

> I've been wanting to do something very similar, only with moon phases.

 

good idea, thanks.  i added your idea to a configuration schemes section.

now, to get nice moon phase photos...

----------

## Danuvius

I don't have an /usr/bin/fbsetbg

Where is that from?

----------

## SerfurJ

/usr/bin/fbsetbg is from fluxbox.  you can use a different background setter (feh, Esetbg, ...), but i'm not sure which other one can set  a background randomly.  there's probably a way to incorporate the randomness using a simple bash script.

----------

## Danuvius

 *SerfurJ wrote:*   

> /usr/bin/fbsetbg is from fluxbox.  you can use a different background setter (feh, Esetbg, ...), but i'm not sure which other one can set  a background randomly.  there's probably a way to incorporate the randomness using a simple bash script.

 

What is the KDE background changer?  And what do I need to emerge to get it?

----------

## alkan

 *Danuvius wrote:*   

>  *SerfurJ wrote:*   /usr/bin/fbsetbg is from fluxbox.  you can use a different background setter (feh, Esetbg, ...), but i'm not sure which other one can set  a background randomly.  there's probably a way to incorporate the randomness using a simple bash script. 
> 
> What is the KDE background changer?  And what do I need to emerge to get it?

 

dcop kdesktop KBackgroundIface setWallpaper /path/to/background/image/file 4

----------

## Danuvius

 *alkan wrote:*   

>  *Danuvius wrote:*    *SerfurJ wrote:*   /usr/bin/fbsetbg is from fluxbox.  you can use a different background setter (feh, Esetbg, ...), but i'm not sure which other one can set  a background randomly.  there's probably a way to incorporate the randomness using a simple bash script. 
> 
> What is the KDE background changer?  And what do I need to emerge to get it? 
> 
> dcop kdesktop KBackgroundIface setWallpaper /path/to/background/image/file 4

 

Doh!  That would have been the second thing I would have tried...  :Laughing: 

Well... no, actually, I never would have guessed.  Thanks.  :Very Happy: 

----------

## SerfurJ

here's a bash script similar to one i saw here for random background setting without fbsetbg:

```
#!/bin/bash

BACKGROUND_DIR=$1

FORMAT="*.jpg"

IMGS=`find "$BACKGROUND_DIR" -iname $FORMAT`

N=`echo $IMGS | wc -w`

((N=RANDOM%N))

echo $IMGS | cut -d ' ' -f $N
```

usage:

```
Esetroot `randomimage /path/to/images`
```

----------

## knobby

SerfurJ,

That script didn't work out too well for me if the filename had spaces in it, which the wallpapers.tar.gz included. :-/

So I took the liberty of changing it around a bit so it would work. This works for me, your mileage may vary.

```

#!/bin/bash

BACKGROUND_DIR=$1

FORMAT="*.jpg"

N=`find "$BACKGROUND_DIR" -iname "$FORMAT"|wc -l`

((N=RANDOM%N))

echo `find "$BACKGROUND_DIR" -iname "$FORMAT"|sed -ne $(expr $N + 1)p`

```

----------

## snakattak3

Oh man, this reminds me of the Noia wallpaper that used to be up on kde-look.org. There was 24 different  wallpapers for each hour of the day. I wish I still had that one to use with this script. Can't seem to find it any more.

[EDIT]

Well, found the page on archive.org

http://web.archive.org/web/20030118230915/www.kde-look.org/content/show.php?content=4203

Thats about it though. Nothing else seems to be up anymore, or archived  :Sad: 

----------

## SerfurJ

thanks knobby.  

 *snakattak3 wrote:*   

> Oh man, this reminds me of the Noia wallpaper that used to be up on kde-look.org. There was 24 different  wallpapers for each hour of the day. I wish I still had that one to use with this script. Can't seem to find it any more.
> 
> [EDIT]
> 
> Well, found the page on archive.org
> ...

 

thanks for the site.  same idea, but it looks like they're using illustrations.

here are some time-series photos i took:

http://www.dunsm.org/photos/2005/nature-series/

----------

## artificio

It'd be really cool if you had a nice camcorder set outside and rigged to take a frame from that every minute or second or so... although I'm not sure if a camcorder can upload and film at the same time.

----------

## BlackB1rd

Thanks, after some modifications I finally got it working with the KDE background changer  :Cool: 

One thing: Why do I almost always get a rainy background? Oh wait, weather always sucks in here  :Mad:   :Wink: 

----------

## Hydraulix

Wow this is great. I'll have to give this a shot when I'm off work.

----------

## Hydraulix

Well I got this error. 

```

Traceback (most recent call last):

  File "/usr/bin/onewithnature", line 25, in ?

    os.popen(bgsetter + wallpaperdir + sys.argv[1] + ' &>/dev/null')

IndexError: list index out of range

```

----------

## tom56

If getWeather returns "None", then the script doesn't like it:

```

Traceback (most recent call last):

  File "./onewithnature", line 20, in ?

    rainy = pattern.search(pr.getWeather())

TypeError: expected string or buffer

```

How does one get around this? I don't know enough python to work out how to fix it.

----------

## japhy

I found a webcam that has a constantly updated image of the area where i live in decent resolution, as I figure many other people could with a little work.  For example, if you live in Berkeley or anywhere near San Francisco: sv.berkeley.edu/view/images/current_view.jpg).  I can handle  adjusting the cron script to a shorter, consistent interval, but does anyone know how to get the script to read an image from a given URL?

----------

## gabe-kai

I am getting a python error when I try to run this script, perhaps someone can help me with it?

./onewithnature

```
Traceback (most recent call last):

  File "./onewithnature", line 25, in ?

    os.popen(bgsetter + wallpaperdir + sys.argv[1] + ' &>/dev/null')

IndexError: list index out of range
```

----------

## da_monumental_1

Anyone set this up to work with gnome?

----------

## larspaul

Have you ever thought about that the sun doesnt sets and rise at the same time the entire year?

You should include that in you script, i don't know any python so i can't.  :Crying or Very sad: 

----------

## Hydraulix

I still get the same error.

----------

## Dlareh

 *Hydraulix wrote:*   

> I still get the same error.

 

onewithnature requires at least one argument, namely the name of a directory within your wallpapers directory

----------

## Dlareh

 *larspaul wrote:*   

> Have you ever thought about that the sun doesnt sets and rise at the same time the entire year?
> 
> You should include that in you script, i don't know any python so i can't. 

 

Alright, this should be fun...

EDIT: see my new post below, I consolidated everything into a single python script

```
emerge -u atd

rc-update add atd default

/etc/init.d/atd start

cd /usr/local/src

wget http://freshmeat.net/redir/sunwait/47299/url_tgz/sunwait-20041208.tar.gz

tar xvzf sunwait-20041208.tar.gz

cd sunwait-20041208

make

mv sunwait /usr/local/bin
```

Now edit  /usr/local/bin/sun and paste the following:

```
#!/bin/bash

LOCATION="43.069886N 89.407534W"

SUN="/usr/local/bin/sunwait -p $LOCATION"

case $1 in

    "rise")

        $SUN | awk '/rises/ {print $3}'

    ;;

    "set")

        $SUN | awk '/rises/ {print $6}'

    ;;

    "noon")

        $SUN | awk '/meridian/ {print $4}'

esac
```

Set LOCATION to your coordinates (in the US, try this site)

Now you can make yourself a script to run once daily from cron.  Here is an example, I saved it as /usr/local/bin/makenature :

```
#!/bin/bash

SUN=/usr/local/bin/sun

COMMAND='pgrep -x X >&/dev/null && /usr/local/bin/onewithnature'

echo "$COMMAND morning >&/dev/null"   | at `$SUN rise` + 10 minutes; sleep 1

echo "$COMMAND mid-day >&/dev/null"   | at `$SUN noon` - 1 hour; sleep 1

echo "$COMMAND afternoon >&/dev/null" | at `$SUN noon` + 1 hour; sleep 1

echo "$COMMAND sunset >&/dev/null"    | at `$SUN set` - 20 minutes; sleep 1

echo "$COMMAND evening >&/dev/null"   | at `$SUN set` + 40 minutes
```

( Replace /usr/local/bin/onewithnature with wherever you put the OP's script )

Of course you can adjust the +/- minutes and hours to suit your tastes.

Don't forget the executable permissions for the scripts:

```
cd /usr/local/bin; chmod 755 sun makenature onewithnature
```

To start makenature at boot, append this line to /etc/conf.d/local.start :

```
su USERNAME /usr/local/bin/makenature >&/dev/null
```

(where USERNAME is the name of the user that will be running X)

Finally, in your USERNAME's crontab you'll only need one line:

```
        1       0       *       *       *       /usr/local/bin/makenature
```

----------

## Dlareh

New python version 0.3 is out, see below...

----------

## grenouille

very nice! thanks  :Very Happy: 

----------

## Dlareh

 *Quote:*   

> very nice! thanks 

 Yay, someone cares... here is the new 0.3 release, code-named grenouille  :Wink: 

Installation -- If this is your first time trying naturalbg, you will need to:

```
emerge -u atd pymetar

rc-update add atd default

/etc/init.d/atd start

cd /usr/local/src

wget http://freshmeat.net/redir/sunwait/47299/url_tgz/sunwait-20041208.tar.gz

tar xvzf sunwait-20041208.tar.gz

cd sunwait-20041208

make

mv sunwait /usr/local/bin
```

The version 0.3 stuff :

```
cd /usr/bin; [ -x fbsetbg ] || wget http://www.khk.org/files/fbsetbg && chmod 755 fbsetbg

cd /usr/local/bin; wget http://www.khk.org/files/naturalbg && chmod 755 naturalbg

$EDITOR naturalbg  #  configure settings . . .
```

Fbsetbg is the default wallpaper setter, but you can use any one you like. It is a wrapper script taken from fluxbox that should work with any windowmanager, and is not needed if you already have fluxbox installed or would like to use something else instead.

Changelog:

- code to detect if an at job has already been scheduled, to avoid scheduling simultaneous changes

- random wallpaper choosing now done internaly instead of fbsetbg -r, so fbsetbg -l  can restore previous wallpaper on X restart

feedback welcome as usual

----------

