Automatically change wallpaper with Nitrogen

How many of you use a "Tiling window manager" and surely after a while our wallpaper bores us? It is very annoying to have to re-compile to be able to change a simple wallpaper, or even to rerun “nitrogen” and pass another parameter to it.

Well today I bring you a script in bash, it gets a random image from our collection and puts it as a wallpaper Change Nitrogen Wallpaper, if you download the script from here don't forget to remove the .txt extension from the file and give it execute permissions

directory = ~ / Images function my_resolution () {res = `xrandr | grep '*' | grep -o -E '[0-9] + x [0-9] +' `my_high =` echo "$ res" | cut -d "x" -f 2` my_width = `echo" $ res "| cut -d "x" -f 1`} function rand_image () {image = `ls -1 -b -R" $ directory "| grep -i -e ".png" -e ".jpg" -e ".jpeg" | sort --random-sort | head -1` image_dir = `find" $ directory "-iname" $ ​​image "`} function image_resolution () {rand_image high_image = `identify -format"% h "" $ image_dir "` wide_image = `identify -format"% w "" $ image_dir "`} function proportion () {my_resolution image_resolution height_rate = `echo $ image_high / $ my_high | bc -l` proportion_width =` echo $ image_width / $ my_width | bc -l`} function scalar () {proportion if [`echo" $ ratio_high> 1 "| bc` -eq 1 -o` echo "$ ratio_width> 1" | bc` -eq 1]; then nitrogen --set-zoom "$ image_dir" else nitrogen --set-centered "$ image_dir" fi} scale exit

Now comes the explanation of the script.
directory = ~ / Images // Change to the directory where your wallpapers are located

image_resolution () // This function detects the resolution of our monitor, and saves it in the variables:
my_high //High
my_width //Width

rand_image () // This function gets an image inside "directory"

image = `ls -1 -b -R" $ directory "| grep -i -e ".png" -e ".jpg" -e ".jpeg" | sort –random-sort | head -1`

IMPORTANT! If you notice we are filtering all files with "extensions" .png, .jpg, .jpeg. Add -e ".extension" as appropriate.

Example:

imagen=`ls -1 -b -R "$directorio" | grep -i -e ".png" -e ".jpg" -e ".jpeg" -e ".nueva_extensión" | sort --random-sort | head -1`

image_resolution () // This function obtains the resolution of the image obtained by the rand_image () function

proportion() // This function gets the ratio image_resolution / screen_resolution

climb() // This function scales the image if it is larger than the resolution of our monitor and sets the wallpaper

Now that we have the script to assign random wallpaper to nitrogen (and also the image adapts to your monitor) we only have to make the script run every so often, this is achieved by executing the following command at startup

watch -n 4 ruta_donde_se_encuentra_el_script/nombre_del_script

In this case, this command is executed every 4 seconds, note that path_where_the_script_is_found is the full path where the script is located and script_name is the name given to the script.

I hope you find it useful.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   KZKG ^ Gaara said

    I will check this Nitrogen for a script for KDE that I want to do :)
    Otherwise good post ... hahahaha, excellent photo you have chosen for the post (Snoopy's LOL !!!)

    1.    alpj said

      Jjajajajjajajaja thank you, indeed snoopy raffle, hahajjajajajjajaj

  2.   st0rmt4il said

    Thank you ..

  3.   WIDTH said

    Thank you

  4.   ferchmetal said

    thanks!

  5.   izzyvp said

    This doesn't work for cinnamon right?

    1.    alpj said

      I have no idea how to change the wallpaper in cinnamon, but the sure thing is that the function rand_image works for you to get a random image, so you just have to pass it $ dir_image
      Let me investigate

  6.   + AMEM + said

    Thank you, I modified it a bit for future generations ... I hope you don't bother
    I would like to know what you think, I don't know much about the language

    #! / Bin / bash

    # ~ Original author
    #~
    # ~ halonsov: Mathematician, GNU / LINUX lover, programmer, Debian and xmonad user.
    #~
    # ~ References
    #~ https://blog.desdelinux.net/cambiar-automaticamente-el-fondo-de-pantalla-con-nitrogen/
    #~
    # ~ Command to run the script every 120 seconds
    # ~ watch -n 120 /home/amem/backup/DataCrunch/Synaptic/RandomWallpaper.sh
    #~
    # ~ Collaboration
    # ~ + AMEM +
    # ~ Personally I prefer to use it only once so as not to use space
    # ~ adding in autostart.sh
    # ~ /home/amem/backup/DataCrunch/Synaptic/RandomWallpaper.sh &
    # ~ Personal note
    # ~ this script was originally made by halonsov.
    # ~ I modified it so that only a little, to execute it at the beginning of #CrunchBang!
    # ~ the purpose is that all those images that you have on your disk you can see them
    # ~ every so often, and that they adjust to the screen according to their properties.
    # ~ for this this script identifies the characteristics of your monitor,
    # ~ and those of the image or photo of your «directory»,
    # ~ in order to execute nitrogen + a scaling or zoom parameter avoiding
    # ~ elongated, pixelated images.
    # ~ If this script does not suit your needs, you can modify its parameters
    # ~ Eg nitrogen –set-centered
    # ~ to center the image without scaling
    # ~ I recommend you study the nitrogen manual.

    #Directory
    #Directory of images to be used as wallpaper, including subdirectories
    #Change to the directory where your wallpapers are located
    # Eg directory = / home /… / images
    directory = / home / amem / images / Wallpaper

    # Monitor resolution
    #This function detects the resolution of our monitor, and saves it in the variables:
    function my_resolution ()
    {
    #We get the resolution of our monitor with xrandr
    res = `xrandr | grep '*' | grep -o -E '[0-9] + x [0-9] +' `
    #Height of the monitor
    my_high = `echo" $ res "| cut -d "x" -f 2`
    # Monitor width
    my_width = `echo" $ res "| cut -d "x" -f 1`
    }

    #Random Image
    #This function gets an image inside "directory"
    function rand_image ()
    {
    #We get the name of some image inside $ directory
    image = `ls -1 -b -R" $ directory "| grep -i -e ".png" -e ".jpg" -e ".jpeg" | sort –random-sort | head -1`

    #IMPORTANT! If you notice we are filtering all files with "extension" .png, .jpg, .jpeg. Add -e ".extension" as appropriate.
    #Example:
    # image = `ls -1 -b -R" $ directory "| grep -i -e ".png" -e ".jpg" -e ".jpeg" -e ".new_extension" | sort –random-sort | head -1`

    #We get the path of $ image
    image_dir = `find" $ directory "-iname" $ ​​image "`
    }

    # Image resolution
    #This function gets the resolution of the image obtained by the rand_imagen () function
    function image_resolution ()
    {
    rand_image
    #We get the height of $ image
    high_image = `identify -format"% h "" $ image_dir "`
    #We get the width of $ image
    wide_image = `identify -format"% w "" $ image_dir "`
    }

    #We get the ratio image_resolution / screen_resolution
    # the result of this division returns a value greater or less than 1
    # proportion 1 the image is larger than the screen
    function proportion ()
    {
    my_resolution
    image_resolution
    ratio_high = `echo $ image_high / $ my_high | bc -l`
    width_proportion = `echo $ width_image / $ my_width | bc -l`
    }

    #We get the ratio image_high / image_width
    # the result of this division returns a value greater or less than 1; Unless the image is square Oo
    # image_proportion 1 the image is taller than it is wide
    function image proportion ()
    {
    image_resolution
    ratio_image_reason = `echo $ tall_image / $ wide_image | bc -l`
    }

    #Set the wallpaper
    # This function modifies the nitrogen parameters according to the properties of the image in relation to the monitor
    #Image too tall or larger than our screen Sets the wallpaper by scaling the image
    # –Set-centered centers the unscaled image
    # –Set-scaled fills the screen with the warped image
    # –Set-zoom fills the screen with the scaled image to the top
    # –Set-zoom-fill fills the screen with the scaled image along
    scalar function ()
    {
    proportion
    if [`echo" $ width_proportion> 1 ″ | bc` -eq 1]; then
    #If the resolution of the image is larger than that of our screen, execute ...
    nitrogen –set-scaled "$ image_dir"
    else
    if [`echo" $ high_rate> 1 ″ | bc` -eq 1]; then
    #If the resolution of the image is smaller than that of our screen, execute ...
    nitrogen –set-centered "$ image_dir"
    else
    #When the image is smaller than the screen we can expand it to length or width
    image proportion
    if [`echo« $ proportionimage_reason> 1 ″ | bc` -eq 1]; then
    #If the height of the image is smaller than that of our screen, execute ...
    nitrogen –set-zoom "$ image_dir"
    else
    if [`echo« $ proportionimage_reason <1 "| bc` -eq 1]; then
    #If the width of the image is smaller than that of our screen, execute ...
    nitrogen –set-zoom-fill "$ image_dir"
    fi
    fi
    fi
    fi
    }

    climb

    exit

    1.    Halonsus V said

      Excellent. Thank you.