You are not logged in.

#1 2015-09-29 14:00:51

tknomanzr
BL Die Hard
From: Around the Bend
Registered: 2015-09-29
Posts: 1,057

An Admittedly Rather Simple Python Wallpaper changer:

This is a simple wallpaper rotater that I wrote. This was prior to me stumbling across @damo's rather elegant solution and was based on the concepts of @corenominal's bash script that just wouldn't work for me. Being that I am not the best bash scripter in the world, I chose to use python. As you can see from the to-do notes, I really want it to be a more robust and transparent tool. Perhaps one day, I will get around to finishing it out. I choose to hook this into my conky and run it once every half hour via this command:

${execpi 1800 rotate-wallpaper}

As you can see the command assumes that a script called rotate-wallpaper is somewhere in your path, as I did not hardcode the link to it.

And the script itself:

#! /usr/bin/env python
## To-Do's:
## Accept arbitray path to images
## Accept .jpg, .png and possibly .svg extensions
## Allow for more than one command to set bg.
## Examples: hsetroot, nitrogen, feh, possibly pcmanfm
## Look into separate wallpapers per display
import os
import random
import sys
my_path="/home/tknomanzr/remote/Shared_Pictures/walls/"
my_extension=[".jpg",".png"]

def directory(path, extension):
	list_dir = []
	list_dir = os.listdir(my_path)
	count = 0
	for file in list_dir:
		for i in range(len(extension)):
			if file.endswith(extension[i]):
				count += 1
		if not list_dir:
			print "No useable image files found."
	return (list_dir, count)
def background(bg):
  cmd = "feh --bg-scale " + bg
  os.system(cmd)
  return
list_dir, count = directory (my_path, my_extension)
file_number = random.randint(0, count-1)
bg_name = my_path + list_dir[file_number] 
background(bg_name)

Offline

#2 2015-10-02 20:45:03

redcollective
Member
From: The Wilds
Registered: 2015-09-29
Posts: 111

Re: An Admittedly Rather Simple Python Wallpaper changer:

Sounds perfect for a giant collection of Google satellite wallpapers: gist

red


Knowledge Ferret

Offline

#3 2015-10-04 16:33:03

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: An Admittedly Rather Simple Python Wallpaper changer:

@tknomanzr:
There is a bug in the script:
1. Create a new directory DIR
2. create 2 files in that dir, in the order specified here:
    touch a.what
    touch b.png
3. Now run your script (after changing my_path to the DIR you created)

Your script will try to set a.what as the wallpaper!

xaos52, 'The unofficial, self-assigned bunsenlabs scripts Quality Assurance Officer'

Offline

#4 2015-10-04 16:42:20

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: An Admittedly Rather Simple Python Wallpaper changer:

@hhh,
Thanks  for making that title official for a very short time, but 'The Good Doctor' makes me proud too. smile

Offline

#5 2015-10-04 16:43:07

tknomanzr
BL Die Hard
From: Around the Bend
Registered: 2015-09-29
Posts: 1,057

Re: An Admittedly Rather Simple Python Wallpaper changer:

Ok. Ill check the logic on it. I intend to flesh it out more at some point. Actually, the more I think on it, the more I realize actually relying on a file extension to be present is somewhat foolish. I am going to give some thought into parsing the output of the file command or see if python has something similar to that command to test the file type.

Offline

#6 2015-10-04 16:45:24

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 8,759
Website

Re: An Admittedly Rather Simple Python Wallpaper changer:

xaos52 wrote:

@hhh,
Thanks  for making that title official for a very short time, but 'The Good Doctor' makes me proud too. smile

Sorry, that was me big_smile

Which title would you prefer?

@tknomanzr -- sorry for the OT


“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.

Forum Rules   •   How to report a problem   •   Software that rocks

Offline

#7 2015-10-04 16:47:57

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: An Admittedly Rather Simple Python Wallpaper changer:

'The Good Doctor' is OK. Thanks.
I saw hhh on-line when I logged in, so I thought he set the title.

Offline

#8 2015-10-04 16:49:24

tknomanzr
BL Die Hard
From: Around the Bend
Registered: 2015-09-29
Posts: 1,057

Re: An Admittedly Rather Simple Python Wallpaper changer:

Looks like pyython has a built in module to handle this. I will try to work on this some soon. I was using this with a collection of Hubble telescope images for the longest.

Offline

#9 2015-10-04 16:50:37

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: An Admittedly Rather Simple Python Wallpaper changer:

@tknomanzr:

Here is something I quickly threw together, that seems to work:

[== Undefined ==]
#! /usr/bin/env python
## To-Do's:
## Accept arbitray path to images
## Accept .jpg, .png and possibly .svg extensions
## Allow for more than one command to set bg.
## Examples: hsetroot, nitrogen, feh, possibly pcmanfm
## Look into separate wallpapers per display

import os
import random
import sys

MY_PATH="/home/me/tmp/today"
MY_EXTENSIONS=[".jpg",".png"]

def select_one_wallpaper(candidates):
        try:
                # select one wall from the candidates
                return random.choice(candidates)
        except IndexError:
                print 'No candidate wallpapers found in directory %s.\n' % (MY_PATH,)
                sys.exit(1)

def set_background(wallpaper):
        cmd = "feh --wall-scale %s" % (os.path.join(MY_PATH, wallpaper))
        retval = os.system(cmd)
        if retval:
                print 'Setting the wallpaper returned an error.'

if __name__ == '__main__':
        candidates = [ x for x in os.listdir(MY_PATH) if os.path.splitext(x)[1].lower() in MY_EXTENSIONS ]
        wallpaper = select_one_wallpaper(candidates)
        set_background(wallpaper)

Last edited by xaos52 (2015-10-04 16:52:54)

Offline

#10 2015-10-04 16:59:05

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: An Admittedly Rather Simple Python Wallpaper changer:

@tknomanzr:

Well your script works OK if the directory contains nothing but files with extensions .jpg or .png.

BTW: I feel 'rotate' is a misnomer for the script, since it randomly selects one from a collection. Perhaps something like 'set-random-wallpaper' is better?

Last edited by xaos52 (2015-10-04 17:03:09)

Offline

#11 2015-10-04 18:04:34

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,382
Website

Re: An Admittedly Rather Simple Python Wallpaper changer:

Any good isImage functions for bash? What I have is usually the "file" command and/or actually doing something with a file

img2txt some.png || echo "probably not an image"

or the somehow ugly grep (hasAudio) < which should fail in a number of imaginary situations, but actually holds in practice

if [ -f "$expanded" ] && mediainfo "$expanded" | egrep -qi 'audio'; then
# stuff
fi

Last edited by brontosaurusrex (2015-10-04 18:08:40)

Online

#12 2015-10-04 18:21:24

damo
....moderator....
Registered: 2015-08-20
Posts: 6,722

Re: An Admittedly Rather Simple Python Wallpaper changer:

Look at the `file` command. Test the if the return is `image`

man file

Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#13 2015-10-04 18:32:34

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,382
Website

Re: An Admittedly Rather Simple Python Wallpaper changer:

The jessie file seems really good and I'am aware of it, see my first line smile

Online

#14 2015-10-04 18:41:12

tknomanzr
BL Die Hard
From: Around the Bend
Registered: 2015-09-29
Posts: 1,057

Re: An Admittedly Rather Simple Python Wallpaper changer:

I found this Python Module that I was thinking about using.

Offline

Board footer

Powered by FluxBB