#! /bin/bash
#! /bin/bash -vx
# input:
# new_background.sh -reset - to reset background to the previous picture
# new_background.sh -set - to set a new background
lastImage=$HOME/.new_background_last_image.txt
DIR="/usr/share/backgrounds"
joinImage=$HOME/.new_background_last_image.jpg
# choose 2 images from dir
number=$(ls -1 $DIR | wc -l | awk '{print $1}')
mapfile -t screenSize < <( xrandr | grep -e "^DP" | awk ' { print $0 }' )
if [ ${#screenSize[@]} != 2 ]
then
echo there don\'t appear to be 2 screens - jobs ends
exit
fi
tot=0
prevH=0
prevW=0
prevFile=""
found=0
SET=-1
case "$1" in
-reset ) SET=0
;;
-set ) SET=1
;;
esac
if [ "$SET" -eq -1 ]
then
echo enter either -set or -reset
exit
fi
# reset previous picture
if [ "$SET" -eq 0 ]
then
URI=$(cat $lastImage)
gsettings set org.gnome.desktop.background picture-options 'zoom'
gsettings set org.gnome.desktop.background picture-uri "${URI}"
exit
fi
# find 2 pictures
while [ $tot -lt $number ]
do
# go throguh files at random until 2 have the same height
randomN=$[ ( $RANDOM % $number ) ]
fileName=$(ls -1 $DIR/* | grep -e jpg -e png | awk 'NR=='$randomN' {print $0}')
if [ x$prevFile == x$fileName ]
then
continue
fi
if [ x"" == x$fileName ]
then
continue
fi
if [ x$prevFile != x$fileName ] && [ x"" != x$fileName ]
then
ww=$(convert $fileName -print "Size: %wx%h\n" /dev/null | awk '{ print $2}')
w=$(echo $ww | awk -F x ' {print $1 }')
h=$(echo $ww | awk -F x ' {print $2 }')
if [ $h -eq $prevH ]
then
found=1
break
fi
fi
prevW=$w
prevH=$h
prevFile=$fileName
tot=$[$tot+1]
done
if [ $found -eq 0 ]
then
echo no files found # may be solved by running the program again
exit
fi
# new width = width of screen * original height of image / height of screen
# = 1920 2160(e.g.) 1080
pref=$(expr $(mktemp -u) : ".*\.\(.*\)")
outputFile=(${pref}_1 ${pref}_2)
files=( $fileName $prevFile )
widths=( $w $prevW)
heights=( $h $prevH)
# do each file
reverse=0 # if the second screen is the first in the screenSize list reverse=1
for I in 0 1;
do
# size of screen I
mapfile -t tt < <(echo ${screenSize[$I]} | grep -E -o '[0-9]+x[0-9]+\+[0-9]+\+[0-9]+' | sed s'/[x+]/ /g' | awk ' { print $1,"\n", $2, "\n", $3, "\n", $4 } ')
# this is the start position of the screen
# if not 0 and I=0 then it is the 2nd screen and reverse images when joining
offset=${tt[2]}
if [ $I == 0 ] && [ $offset != 0 ]
then
reverse=1
fi
let "newWidth = ${heights[$I]}*${tt[0]}/${tt[1]}"
convert -resize ${newWidth}x${heights[$I]}! ${files[$I]} ${outputFile[$I]}
done
# join the files
if [ $reverse == 0 ]
then
convert +append ${outputFile[0]} ${outputFile[1]} $joinImage
else
convert +append ${outputFile[1]} ${outputFile[0]} $joinImage
fi
URI=$(gsettings get org.gnome.desktop.background picture-uri)
# save the previous file name
echo $URI > $lastImage
uri=file:"//$joinImage"
gsettings set org.gnome.desktop.background picture-options 'spanned'
gsettings set org.gnome.desktop.background picture-uri "${uri}"
rm ${outputFile[0]} ${outputFile[1]}
# end-of-file
这可能是您正在寻找的。
我使用“bash”、“convert”和“gsettings”来完成。它适用于我的 2 个屏幕。
概述:
我正在运行 Ubuntu 18.04。随意破解您的内心