wallpaper resizing

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
666philb
Posts: 3615
Joined: Sun 07 Feb 2010, 12:27
Location: wales ... by the sea

wallpaper resizing

#1 Post by 666philb »

in older pups there was a script called by the wallpaper setter 'set_bg' called 'background_reshape'.

this would reshape a wallpaper in /usr/share/backgrounds to fit the dimensions of the screen by shaving off equal amounts of the top & bottom of the image. puppys generally small 4:3 ratio wallpaper then could be stretched to fit bigger 16:9 screens without distorting.

i would like it to do it a little differently. i have a 4k 16:9 ratio wallpaper in fossapup and would like the script to shave off the sides of the image so that it doesn't look distorted on laptops etc.

just looking at this script melts my brain :D

Code: Select all

#!/bin/ash
#Barry Kauler 2009
#the background image is at /usr/share/backgrounds/default.jpg
#this may have the wrong dimensions to suit either a 'normal' or a 'widescreen' monitor.
#for now, keep this simple, assume image is suitable for normal screen, truncate vertically
#if widescreen.
#v424 fix maths.
#v424 move ORIGINAL-* images to /usr/share/backgrounds_original
#110831 support png images.
#111013 Karl Godt: 'dc' can return , instead of . in numeric values, for non-english locale.
#130326 Karl Godt: Added MODE option to be able to swith between math by BK and math by /bin/sh.
#130327 Doubble Quotes around Files and Directories to support Spaces.
#130327 Karl Godt: Logfile is /tmp/backg.log.
#130327 wjaguar: Rewrote unnecessarily tortuous math & string handling

LOG=/tmp/backg.log
exec 1>>$LOG 2>&1
: << 'COMMENTED_OUT' # Mode not used anymore
[ "$DEBUG" ] && echo "DEBUG=$DEBUG" 
#below set this if wanted (un-comment by deleting the prceeding # (number-sign):
#MODE=old 
[ "$MODE" ] || {
	case "$1" in
	"-m" ) MODE=$2 ; shift 2 ;;
	"-m"* ) MODE=${1#-m} ; shift 1 ;;
	"--mode="* ) MODE=${1#--mode=} ; shift 1 ;;
	esac
	FILENAME=$1
}
echo "MODE=$MODE"
echo "FILENAME=$FILENAME"
COMMENTED_OUT

#accept optional image file as passed param...
[ -f /usr/share/backgrounds/default.png ] && IMAGEFILE="/usr/share/backgrounds/default.png"
[ -f /usr/share/backgrounds/default.jpg ] && IMAGEFILE="/usr/share/backgrounds/default.jpg"
#[ $1 ] && IMAGEFILE="$1"
[ "$*" ] && IMAGEFILE="$@"
[ "$IMAGEFILE" = "" ] && exit
echo "IMAGEFILE=$IMAGEFILE"
#v423 precaution, if someone has chosen the backup original...
[ "$IMAGEFILE" != "${IMAGEFILE#ORIGINAL}" ] && exit

[ -f "$IMAGEFILE" ] || { echo "$0: $IMAGEFILE is not a file ..?"; exit; }

IMAGEPATH=${IMAGEFILE%/*}
[ "$IMAGEPATH" = "$IMAGEFILE" ] && IMAGEPATH=`pwd`
mkdir -p "${IMAGEPATH}_original" #v424
IMAGEBASE=${IMAGEFILE##*/}
case $IMAGEBASE in
*.jpg|*.jpeg|*.JPG|*.JPEG) IMAGEPNM='jpegtopnm'
	PNMIMAGE="pnmtojpeg -quality=85" ;;
*.png|*.PNG) IMAGEPNM='pngtopnm'
	PNMIMAGE="pnmtopng" ;;
*) echo "$0: Only support for PNG and JPG files" 
	exit ;;
esac
echo "IMAGEPATH=$IMAGEPATH IMAGEBASE=$IMAGEBASE IMAGEPNM=$IMAGEPNM"

# Safety check
which $IMAGEPNM >/dev/null 2>&1 || { echo "No $IMAGEPNM utility!" ; exit ; }
which ${PNMIMAGE%% *} >/dev/null 2>&1 || { echo "No $PNMIMAGE utility!" ; exit ; }

#first boot, maybe trying different video modes, so this causes reset each time...
if [ ! -f "${IMAGEPATH}_original/ORIGINAL-$IMAGEBASE" ];then #v424
	cp -f "$IMAGEFILE" "${IMAGEPATH}_original/ORIGINAL-$IMAGEBASE" #v424
else
	cp -f "${IMAGEPATH}_original/ORIGINAL-$IMAGEBASE" "$IMAGEFILE" #restore. v424
fi

read IMAGEHORIZ IMAGEDIMS IMAGEVERT IMAGEDIMS << EOF
`$IMAGEPNM "$IMAGEFILE" 2>/dev/null | pamfile 2>&1 | grep 'stdin:' | cut -f 2 -d ','`
EOF
echo "IMAGEHORIZ=$IMAGEHORIZ IMAGEVERT=$IMAGEVERT"

read ROOTDIMS ROOTHORIZ ROOTVERT << EOF
`xwininfo -root | grep ' \-geometry ' | cut -f 1 -d '+' | tr 'x' ' '`
EOF

#v423 some math gymnastics involved here..
IMGVERTSCALED=$(( ( $ROOTHORIZ * $IMAGEVERT ) / $IMAGEHORIZ ))
NEWVERT=$(( ( $IMAGEHORIZ * $ROOTVERT ) / $ROOTHORIZ )) #v424 fix.
 
echo "IMGVERTSCALED=$IMGVERTSCALED NEWVERT=$NEWVERT" #TEST

#this method cuts equal amount from top and bottom...
if [ "$NEWVERT" -gt "0" ] && [ $NEWVERT -lt $IMAGEVERT ] #precaution
then
	PAMCUT=pamcut
	which $PAMCUT >/dev/null 2>&1 || PAMCUT=pnmcut #older netpbm.
	which $PAMCUT >/dev/null 2>&1 || exit # no netpbm at all
	CUTVERT=$(( $IMAGEVERT - $NEWVERT ))
	$IMAGEPNM "${IMAGEPATH}_original/ORIGINAL-$IMAGEBASE" | \
		$PAMCUT -left=0 -top=$(( $CUTVERT / 2 )) -height=$NEWVERT | \
		$PNMIMAGE > "$IMAGEFILE"
fi

###END###
Bionicpup64 built with bionic beaver packages http://murga-linux.com/puppy/viewtopic.php?t=114311
Xenialpup64, built with xenial xerus packages http://murga-linux.com/puppy/viewtopic.php?t=107331

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

#2 Post by step »

I changed quite a few things in Nathan's Wallpaper Setter for Fatdog64, including background-reshape. Take a look at my github.

Fatdog64's wallpaper setter can reshape the wallpaper in three ways:
1) stretch short and long side unproportionally to cover the whole screen area (rox's "stretch" method does this)
2) stretch the short/long side to the screen height/width to fill that side and leave unfilled side bands along the other side
3) stretch the wallpaper proportionally to cover the whole area.

The last method will effective cut out excess on either the long or the short side, whichever has to stretch percentage-wise the least compared to the other side. This method should work for you. Try it on Fatdog64, one picture's worth...
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

User avatar
666philb
Posts: 3615
Joined: Sun 07 Feb 2010, 12:27
Location: wales ... by the sea

#3 Post by 666philb »

that's perfect!

thanks step
Bionicpup64 built with bionic beaver packages http://murga-linux.com/puppy/viewtopic.php?t=114311
Xenialpup64, built with xenial xerus packages http://murga-linux.com/puppy/viewtopic.php?t=107331

User avatar
Jbond
Posts: 3
Joined: Mon 20 Apr 2020, 17:06

#4 Post by Jbond »

I would not say that I have a good command of templates, but if you wish, you can download cs 1.6 and have a great time.

Post Reply