You are not logged in.

#1 2011-05-02 21:37:36

kurrata
Member
Registered: 2009-04-10
Posts: 8

set random image from directory to background with feh

well my 1st python script if some one cares
As title says. it takes random image from directory and sets it as desktop background

##
#Sets your background to random image from directory using feh
##
import os
import random
import subprocess

IMAGE_DIRECTORY = "/home/martins/.wallpaper/"
IMAGE_FORMATS = ['jpg', 'png']

ALL_IMAGES = []

def getAllImages ():
    root = os.listdir(IMAGE_DIRECTORY)
    for myFile in root:
        isImage = False
        for myFormat in IMAGE_FORMATS:
            if(myFile[-3:] == myFormat):
                isImage = True
            if (isImage):
                path = IMAGE_DIRECTORY + myFile
                ALL_IMAGES.append(path)
                isImage = False
                break

def selectImage ():
    imageCount = len(ALL_IMAGES)
    return ALL_IMAGES[random.randint(0, imageCount-1)]
 
getAllImages()
selectedImage = selectImage()
subprocess.call(('feh', '--bg-fill', selectedImage))

Offline

#2 2011-05-02 22:04:00

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: set random image from directory to background with feh

I think there should be a line saying it's a python script, something like

#!/usr/bin/python

otherwise I get a bunch of errors

./t1: line 4: import: command not found
./t1: line 5: import: command not found
./t1: line 6: import: command not found
./t1: line 8: IMAGE_DIRECTORY: command not found
./t1: line 9: IMAGE_FORMATS: command not found
./t1: line 11: ALL_IMAGES: command not found
./t1: line 13: syntax error near unexpected token `('
./t1: line 13: `def getAllImages ():'

Does it use python2 or 3?


Edit: I assume that this script is an exercise in python programming, because it seems there are simpler ways of getting a random wallpaper https://wiki.archlinux.org/index.php/Fe … ound_image

Last edited by karol (2011-05-02 22:11:58)

Offline

#3 2011-05-02 22:51:56

kurrata
Member
Registered: 2009-04-10
Posts: 8

Re: set random image from directory to background with feh

yes, it is exercise to learn some python. so any feedback is welcome

Offline

#4 2011-05-04 15:42:40

Tes--
Member
Registered: 2009-11-13
Posts: 44

Re: set random image from directory to background with feh

I'm very bored at work right now, so here is some feedback. Don't take it the wrong way, it's meant as constructive feedback.

In your method getAllImages, you loop over every specified extension. I have two things to say about this:
1. You can check if an object is in a list with the "in" statement. That would allow you to write:

if myFile[-3:] in IMAGE_FORMATS:
  .. file is an image

2.

if (myFile[-3:] == myFormat):
  isImage = True
if (isImage):
  .. file is an image
  break

This is equivalent to

if (myFile[-3:] == myFormat):
  .. file is an image
  break

which has the benefit of not using an extra boolean isImage that you have to keep track of.

Python also has quite a large standard library, so if you have to do something that seems simple, there is probably some method somewhere:

selectedImage = random.choice(ALL_IMAGES)

Also: getAllImages writes to a global variable, please please please keep your use of global variables to a minimum. In this case it would be better if it returned a list instead. The same holds for selectImage, which uses a global variable as input.

So keep up, there are still a few other things you could do and look out for, but since you're still learning I'll keep them to myself.

Offline

Board footer

Powered by FluxBB