123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- #!/bin/bash
- [ ! -t 0 ] && exit 0
- # test: is this being executed inside a terminal?
- # get a better impression of tiled images while browsing through
- # folders full of them, recursively. change background color for transparent tiles,
- # and use the resulting image as background (tiled).
- #
- # you may pass a different path as an argument, otherwise the current folder will
- # be searched for images.
- #
- # requires imagemagick and feh or fbsetbg for bg setting, and grabc (command line color picker)
- # shell & terminal stuff
- trap 'exit_cleanup' EXIT
- shopt -u nocasematch # just normalizing
- ################################################################################
- # FUNCTIONS
- function full_size_manip {
- # a set of manipulations that require a screen-sized canvas to be created from the tile.
- while true ; do
- screeninit "Submenu for ${array[c]}"
- #~printf "${red}No quality control for the imagemagick options! The defaults provide gaussian blur + radial gradient with low opacity (centered lighting). Also see: http://imagemagick.org/script/command-line-options.php$rst\n${blu}s: Change canvas size (current: $tempres)\n${mag}b: Toggle blur (current: $rst$rvs"
- printf "s: Change canvas size (current: $tempres)
- ${mag}b: Toggle blur ($toggleblur)"
- if [[ "$toggleblur" == "ON" ]]; then
- printf " - blur radius: $blurradius
- \t1: Change Blur Radius
- \t2: Change complete blur option"
- fi
- printf "\nl: Toggle lighting ($togglelighting)\n"
- if [[ "$togglelighting" == "ON" ]]; then
- printf "\t3: Change lighting settings - currently:
- \t inner color: %s; outer color: %s
- \t opacity: %s; compose method: %s
- \t4: Change complete lighting & composite option (tricky!)\n" "$innercolor" "$outercolor" "$opacity" "$composemethod"
- fi
- printf "r: Redraw current preview
- A: Apply settings, then $message
- q,Space: Back to main menu
- R: Restore all values to their defaults\n"
- case $(prompt) in
- s) printf "\nEnter canvas size, e.g. 1024x768, both width and height: min. 100, max 99999\n<Enter> to leave unchanged\n"
- until [[ "$x" =~ ^[0-9]{3,5}x[0-9]{3,5}$ ]] || [[ "x$x" == "x" ]]; do
- read -p ">>> " x
- done
- [[ "x$x" != "x" ]] && [[ "$x" != "r" ]] && tempres="$x"
- ;;
- b) [[ "$toggleblur" == "ON" ]] && toggleblur="OFF" || toggleblur="ON"
- ;;
- 1) if [[ "$toggleblur" == "ON" ]]; then
- printf "\nEnter blur radius (current: $blurradius, <Enter> leaves it as it is): "
- read x
- [[ "x$x" != "x" ]] && blurradius="$x"
- apply_values
- else
- wrong_input
- fi
- ;;
- 2) if [[ "$toggleblur" == "ON" ]]; then
- printf "\nEnter blur option (current: \"$blur\", <Enter> leaves it as it is): "
- read x
- [[ "x$x" != "x" ]] && blur="$x"
- else
- wrong_input
- fi
- ;;
- l) [[ "$togglelighting" == "ON" ]] && togglelighting="OFF" || togglelighting="ON"
- ;;
- 3) if [[ "$togglelighting" == "ON" ]]; then
- printf "\nEnter inner color - "
- change_color "innercolor"
- printf "Enter outer color - "
- change_color "outercolor"
- read -p "Enter opacity (current: $opacity, <Enter> leaves it as it is): " x
- [[ "x$x" != "x" ]] && opacity="$x"
- read -p "Enter compose method (current: $composemethod, <Enter> leaves it as it is): " x
- [[ "x$x" != "x" ]] && composemethod="$x"
- apply_values
- else
- wrong_input
- fi
- ;;
- 4) if [[ "$togglelighting" == "ON" ]]; then
- printf "\nEnter lighting & composite option\ncurrent:\n\"$lighting\"\n<Enter> leaves it as it is: "
- read x
- [[ "x$x" != "x" ]] && lighting="$x"
- else
- wrong_input
- fi
- ;;
- A) [[ "$togglelighting" == "OFF" ]] && lighting=""
- [[ "$toggleblur" == "OFF" ]] && blur=""
- echo
- if convert -size "$tempres" -quality 99 tile:"$tmptile" $blur $lighting "${output%.*}".jpg ; then
- set_bg full "${output%.*}.jpg"
- fi
- ;;
- ' '|q)
- return
- ;;
- R) set_defaults
- ;;
- r)
- ;;
- *) wrong_input
- ;;
- esac
- done
- }
- function setwindowsize {
- wmctrl -r :ACTIVE: -b remove,maximized_vert,maximized_horz
- wmctrl -r :ACTIVE: -e 0,-1,-1,600,600
- tabs 10 +3 +3 +3 +3 +3 +3 +3 +3 # setting tabs - first is longer, rest is shorter than the default 8
- }
- function change_color { # takes the name of the variable to be filled
- printf "\ncurrent color: ${!1}\n"
- x=''
- #~ until [[ "$x" == \#+([0-9a-fA-F]) && ${#x} == @(4|7|13) ]]; do
- until [[ "$x" =~ ^#([[:xdigit:]]{3}|[[:xdigit:]]{6}|[[:xdigit:]]{12})$ ]]; do
- printf "New color in hex format #rgb or #rrggbb or #rrrrggggbbbb\nEnter manually, "
- [ -n "$colpick" ] && printf "enter p to pick, "
- printf "or just enter to cancel): "
- read x
- [ -z "$x" ] && return
- [[ "$x" == "p" ]] && [ -n "$colpick" ] && x="$(${colpick}"${!1}" 2>/dev/null)"
- done
- declare -g $1="$x" # stackoverflow.com/a/27049256
- }
- function apply_values {
- lighting="( -size $tempres radial-gradient:${innercolor}-${outercolor} -spread 10 -alpha set -channel a -evaluate set $opacity +channel ) -compose $composemethod -composite"
- blur="-gaussian-blur $blurradius"
- }
- function set_defaults {
- tempres="$res"
- innercolor="#FFF7D9"
- outercolor=black
- opacity="70%"
- composemethod=multiply
- togglelighting="ON"
- blurradius="4x1"
- toggleblur="OFF"
- apply_values
- }
- function drawimage {
- # http://blog.z3bra.org/2014/01/images-in-terminal.html
- echo -e "0;1;0;$imgoffset;$imgwidth;$imgheight;2;2;;;$preview\n4;\n3;" | $w3mimgdisplay -x 0 -y 0 >/dev/null 2>&1
- # also see /usr/share/doc/w3m/README.img
- }
- function exit_cleanup {
- clear
- rm -rf "$tmpdir"
- exit
- }
- function wrong_input {
- printf " - wrong input"
- sleep 0.3
- printf "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
- tput el
- }
- function erase { # erases the character entered until end of line
- printf "\b"
- tput el
- }
- function set_bg {
- # 1st argument: action 2nd argument: imagefile
- case $1 in
- prepare)
- case $bg_setter in
- feh)
- cp -f ~/.fehbg "$tmpdir/"
- ;;
- fbsetbg)
- cp -f ~/.fluxbox/lastwallpaper "$tmpdir/"
- ;;
- save)
- ;;
- esac
- ;;
- restore)
- case $bg_setter in
- feh)
- cp -f "$tmpdir/.fehbg" ~/.fehbg
- . ~/.fehbg
- ;;
- fbsetbg)
- cp -f "$tmpdir/lastwallpaper" ~/.fluxbox/
- fbsetbg -l
- ;;
- save)
- ;;
- esac
- ;;
- tiled)
- case $bg_setter in
- feh) feh --bg-tile "$2"
- ;;
- fbsetbg) fbsetbg -t "$2"
- ;;
- save)
- printf "background file saved as $2\n"
- ;;
- esac
- ;;
- full)
- case $bg_setter in
- feh) feh --bg-fill "$2"
- printf "background set with $bg_setter\nfile saved as $2\nThis action can be reversed in the main menu.\n"
- anykey
- ;;
- fbsetbg) fbsetbg -f "$2"
- printf "background set with $bg_setter\nfile saved as $2\nThis action can be reversed in the main menu.\n"
- anykey
- ;;
- save)
- printf "background file saved as $2"
- if [ -e "${img_display%% *}" ]; then
- printf "\nPress any key to see full screen preview, q to skip "
- read -n1 x
- if [[ "$img_display" == */feh ]]; then
- [[ "$x" != "q" ]] && $img_display -N -q --info 'printf "Filename: %f\nSize: %wx%h - %S\nPress q or ESC to exit" ' -Y -F -N --draw-tinted "$2"
- else
- [[ "$x" != "q" ]] && $img_display "$2"
- fi
- fi
- ;;
- esac
- ;;
- esac
- }
- function clearstdin { # superuser.com/questions/276531/clear-stdin-before-reading
- while read -r -t 0; do read -r; done
- #~read -d '' -t 0.1 -n 10000
- }
- function anykey {
- read -n1 -p "Press any key to continue... "
- }
- function screeninit {
- clear
- drawimage
- #~tput cup 0 0
- #~tput sgr0
- printf "\033]0;%s\007" "$me - $1"
- }
- function prompt {
- local x
- oldifs="$IFS"
- IFS=$'\n'
- clearstdin
- read -n1 -p ">>> " x
- clearstdin
- IFS="$oldifs"
- printf "$x"
- }
- ################################################################################
- ################################################################################
- # MAIN
- # define some default variables
- me="$(basename $0)"
- uuid="$me$(date +%y%m%d%H%M%S)"
- alpha="$uuid" # just some control variable
- outfile="$uuid.png"
- outdir="$HOME/.config/tileset"
- output="$outdir/$outfile"
- tmpdir="/tmp/$uuid"
- tmptile="$tmpdir/tmptile.png"
- preview="$tmpdir/preview.png"
- imgwidth=600
- imgheight=450
- imgoffset=150
- shuffle=1 # randomize array
- parse_hidden=0 # set to 1 to also process files and folders starting with a '.'
- bgcol="#363D4D" # just some default color (greyish-blue)
- for colpick in "grabc " "zenity --color-selection" "yad --color"
- do
- which $colpick >/dev/null 2>&1 && break || colpick=""
- done
- img_display="$(which feh 2>/dev/null)"
- w3mimgdisplay=/usr/lib/w3m/w3mimgdisplay
- bg_setter=feh # one of: fbsetbg feh save
- if [[ "$bg_setter" == "save" ]]; then message="save"; else message="set as desktop background"; fi
- # screen resolution
- res="$(xprop -notype -root '$0x$1' _NET_DESKTOP_GEOMETRY)"
- res="${res##*_NET_DESKTOP_GEOMETRY}"
- ##### START DOING THINGS #####
- mkdir -p "$outdir"
- mkdir -p "$tmpdir"
- set_bg prepare
- setwindowsize
- # change to directory, if given on command line
- if [ -d "$1" ]; then
- cd "$1"
- fi
- oldifs="$IFS"
- IFS=$'\n'
- # or, if a single file is given as argument, set array to that single file.
- if [ -f "$1" ]; then
- array=("$1")
- else
- if [[ "$parse_hidden" == "1" ]]; then
- array=($(find -iname '*.png' -print -o -iname '*.gif' -print -o -iname '*.jpg' -print -o -iname '*.bmp' -print -o -iname '*.jpe' -print -o -iname '*.jpeg' -print -o -iname '*.tga' -print -o -iname '*.tiff' -print -o -iname '*.xbm' -print -o -iname '*.xpm' -print))
- else # excludes hidden (dot) directories (and files?)
- array=($(find -path '*/.*' -prune -o -iname '*.png' -print -o -iname '*.gif' -print -o -iname '*.jpg' -print -o -iname '*.bmp' -print -o -iname '*.jpe' -print -o -iname '*.jpeg' -print -o -iname '*.tga' -print -o -iname '*.tiff' -print -o -iname '*.xbm' -print -o -iname '*.xpm' -print))
- fi
- if [[ "$shuffle" == 1 ]]; then
- array=( $(for i in "${array[@]}"; do echo "$i"; done | shuf) )
- fi
- fi
- IFS="$oldifs"
- set_defaults
- ###############################################################################
- ########## MAIN LOOP (UNTIL END OF ARRAY) ###########
- for (( c=0 ; c< ${#array[*]} ; )); do
- if [[ "$alpha" == "$uuid" ]]; then
- # let's decide whether the image has an alpha channel or not:
- shopt -s nocasematch
- if [[ "${array[c]}" == *.jp? ]] || [[ "${array[c]}" == *.jpeg ]]
- then
- alpha=False
- else
- alpha="$(identify -format "%A " "${array[c]}" 2>/dev/null)"
- alpha="${alpha%% *}" # animated gifs return multiple values, we only need the 1st
- fi
- [[ "$alpha" != "False" ]] && alpha=1 # there's no harm in making this assumption
- shopt -u nocasematch
- fi
- if [[ "$alpha" == 1 ]]; then
- # create temporary tile with bg color
- convert -size "$(identify -format "%wx%h" "${array[c]}")" -background "$bgcol" xc:"$bgcol" "${array[c]}" -layers flatten "$tmptile"
- else
- tmptile="${array[c]}" # tmptile is only ever read, never written to, so
- # we can set it to the original image if it doesn't have an alpha channel
- fi
- title="$((c +1))/${#array[*]}: ${array[c]#./} ($(identify -format "%wx%h" "${array[c]}")px)"
- #~title="Array $((c +1))/${#array[*]}: ${array[c]}\n$(file -b "${array[c]}")"
- # create a tiled preview canvas to be displayed with w3mimgdisplay
- convert -size "${imgwidth}x${imgheight}" tile:"$tmptile" "$preview"
- while true ; do
- screeninit "$1"
- printf "$title\n"
- [[ $alpha == 1 ]] && printf "b: Change tile background color (currently: $bgcol)\n"
- [[ $alpha != 1 ]] && [[ "$bg_setter" != "save" ]] && printf "o: Set original file as desktop background, tiled\n"
- [[ $alpha == 1 ]] && printf "n: Create new tile with BG color as seen, then $message\n"
- printf "s: Submenu: create screen-sized canvas...\nr: Redraw current preview\nR: Restore previous (before starting $me) background\nSpace: Next image\nBkspc: Previous image\n.: 10 images forward\n,: 10 images backward\n0,q: Quit\n"
- case $(prompt) in
- b) if [[ $alpha == 1 ]]; then
- change_color "bgcol" # it's the name of the variable to be filled
- break
- else
- wrong_input
- fi
- ;;
- o) if [[ $alpha != 1 ]]; then
- set_bg tiled "${array[c]}"
- erase
- else
- wrong_input
- fi
- ;;
- n) if [[ $alpha == 1 ]]; then
- cp -f "$tmptile" "$output"
- set_bg tiled "$output"
- erase
- else
- wrong_input
- fi
- ;;
- s) full_size_manip
- ;;
- r) drawimage
- erase
- ;;
- R) set_bg restore
- erase
- ;;
- 0|q)
- exit_cleanup
- ;;
- ' ')
- c=$((c + 1))
- break
- ;;
- ) # backspace = previous image
- c=$((c - 1))
- break
- ;;
- ,) # 10 images backward
- c=$((c - 10))
- break
- ;;
- .) # 10 images forward
- c=$((c + 10))
- break
- ;;
- *) wrong_input
- ;;
- esac
- done
- done
- exit_cleanup
|