Skip to content

Instantly share code, notes, and snippets.

@arpanpal010
Last active December 5, 2023 16:02
Show Gist options
  • Save arpanpal010/92a6e7a4892dd690362e to your computer and use it in GitHub Desktop.
Save arpanpal010/92a6e7a4892dd690362e to your computer and use it in GitHub Desktop.
wallpaper cropper and setter for awesomeWM, also generates theme and shell colors
#!/bin/bash
# default location of theme and wallpaper
# replace 'awtheme' with your current theme
wallpaperDestination="$HOME/.config/awesome/themes/awtheme/wallpaper";
# add #include "/home/arch/.Xresources_colors" to .Xresources
colorScriptDestination="/home/arch/.Xresources_colors"
# add [[ -f "$HOME/.bash_colors" ]] && . "$HOME/.bash_colors"; to .bashrc
# bashColorsDestination="/home/arch/.bash_colors"
# add dofile(themes .. themename .. "/colors.lua") to theme.lua of current theme e.g awtheme
awesomeWMColorsDestination="/home/arch/.config/awesome/themes/awtheme/colors.lua"
# helper functions
# mkpng.py to crop wallpaper to screen size # need python2-PILlow
locationMkPNG="$HOME/bin/mkpng.py";
# colors to generate dominant colors from wallpaper # git://git.2f30.org/colors # http://blog.z3bra.org/2015/06/vomiting-colors.html
locationColors="$HOME/software/git/colors/colors";
wallpaperSource=$(readlink -f $1);
wallpaperTempLocation="/tmp/tmp.png";
# wallpaper setter for awesome
awSetWallpaper () {
if [ -f "$1" ]; then
local wallpath="$(readlink -f "$1")"
# local wallpaperTempLocation="/tmp/tmp.png";
echo "Resizing $wallpath as tmp.png in /tmp";
"$locationMkPNG" "$wallpath" "$wallpaperTempLocation";
# replace the default wallpaper @wallpaperDestination with the current image
if [ $(cp "$wallpaperTempLocation" "$wallpaperDestination" && echo 1 || echo 0) == 1 ]; then
echo "Set wallpaper: $wallpath";
else
echo "Cannot set wallpaper.";
fi;
else
echo "Image not found: $1";
return 1;
fi;
}
# generate color script from /tmp/tmp.png using colors
genColorScripts () {
if [ ! -f "$1" ]; then
echo "Image not found: $1";
return 1;
fi;
local CPT=0;
[[ -f /tmp/colorList ]] && rm /tmp/colorList;
[[ -f "$colorScriptDestination" ]] && rm "$colorScriptDestination";
# [[ -f $bashColorsDestination ]] && rm $bashColorsDestination;
[[ -f "$awesomeWMColorsDestination" ]] && rm "$awesomeWMColorsDestination";
echo "Generating colors for term and awesomeWM"
echo "! source: $wallpath" > "$colorScriptDestination";
# echo "# source: $wallpath" > "$bashColorsDestination";
echo "-- source: $wallpath" > "$awesomeWMColorsDestination";
"$locationColors" -pevn 16 "$1" 1>/tmp/colorList;
[[ $? != 0 ]] && exit; # above fails if image is not PNG, so exit in that case
# $locationColors -en 16 $1 | sort -dbf | cat > /tmp/colorList;
# $locationColors -en 16 $1 | shuf | cat > /tmp/colorList;
# $locationColors -pern 16 $1 | shuf | cat > /tmp/colorList;
while read HEXCODE;
do
# .Xresources_colors
printf '*color%d: %s\n' "$CPT" "$HEXCODE" >> "$colorScriptDestination";
# .bashrc_colors
# printf 'export COLOR%d=\"%s\"\n' "$CPT" "$HEXCODE" >> $bashColorsDestination;
printf 'theme.color%d=\"%s\"\n' "$CPT" "$HEXCODE" >> "$awesomeWMColorsDestination";
CPT=$(expr $CPT + 1)
done < /tmp/colorList;
}
# post setting wallpaper, reloading stuff done here
reloadConfiguration () {
# load .Xresources
echo "Reloading .Xresources"
xrdb -load ~/.Xresources;
# echo "Loading colors in .bashrc
# source ~/.bashrc;
# kill xautolock if running, it'll get started again by awesome
pgrep 'xautolock' > /dev/null && (
echo "Killing xautolock, will be restarted by awesome"
pkill 'xautolock';
);
echo "Restarting awesome..."
# echo 'awesome.restart() | awesome-client'
pkill -HUP awesome;
# dont forget to exit
exit;
}
[[ "$1" == "w" && ! -z "$2" ]] && awSetWallpaper "$2" && reloadConfiguration;
[[ "$1" == "c" && ! -z "$2" ]] && genColorScripts "$2" && reloadConfiguration; # only works with PNG
# only image as $1
[[ $# == 1 ]] && awSetWallpaper "$1" && genColorScripts "$wallpaperTempLocation" && reloadConfiguration;
#!/bin/python2.7
import os, sys, re
from PIL import Image, ImageChops, ImageOps
def show_usage () :
print("""Simple JPEG to monitor sized wallpaper generator\nDeps: PIL(2.7)\nUsage:\n[python2.7] %s /path/to/image [/path/to/outfile] [WIDTHxHEIGHT]\n""" % (sys.argv[0],))
def makeThumb(f_in, f_out, size=(80,80), pad=False):
image = Image.open(f_in)
image.thumbnail(size, Image.ANTIALIAS)
image_size = image.size
if pad:
thumb = image.crop( (0, 0, size[0], size[1]) )
offset_x = max( (size[0] - image_size[0]) / 2, 0 )
offset_y = max( (size[1] - image_size[1]) / 2, 0 )
thumb = ImageChops.offset(thumb, offset_x, offset_y)
else:
thumb = ImageOps.fit(image, size, Image.ANTIALIAS, (0.5, 0.5))
thumb.save(f_out)
def main () :
if len(sys.argv)>1 and os.path.isfile(sys.argv[1]) :
infile = sys.argv[1]
if(len(sys.argv)>3) :
width, height = [ int(s) for s in re.split(r'[xX]', sys.argv[3])]
else :
screen = os.popen("xrandr -q -d :0").readlines()[0]
height=int(screen.split()[9][:-1])
width=int(screen.split()[7])
outfile = sys.argv[2] if len(sys.argv)>2 else os.path.splitext(infile)[0] + unicode(width) +"x" + unicode(height) + ".png"
print(infile, outfile)
print(unicode(width) + "x" + unicode(height))
makeThumb(infile, outfile, (width, height), False)
else :
show_usage()
if __name__ == "__main__" :
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment