Skip to content

Instantly share code, notes, and snippets.

@wolkenschieber
Last active May 21, 2018 19:35
Show Gist options
  • Save wolkenschieber/763bcd6b06255e26a8d4825a02563692 to your computer and use it in GitHub Desktop.
Save wolkenschieber/763bcd6b06255e26a8d4825a02563692 to your computer and use it in GitHub Desktop.
Fit pictures to digital photoframe
#! /usr/bin/env bash
# Fit pictures to digital photoframe
#Samsung SPF-107H
#================
#
#Resolution: 1024x600
#
#Fit height:
#-----------
#mogrify -resize x600 <imagefile>
#
#Fit width:
#----------
#mogrify -resize 1024 <imagefile>
#
#Fit resolution (keep aspect ration und choose, what fits best):
#--------------
#mogrify -resize 1024x600 <imagefile>
#
#Fixed resolution (destroy aspect ration):
#-----------------
#mogrify -resize 1024x600! <imagefile>
#Set internal field separator
IFS=$(echo -en "\n\b")
#Screen dimensions
SCREENWIDTH=1024
SCREENHEIGHT=600
for file in `find . -type f -iname "*.png"`;
do
echo "Converting $file to $file.jpg"
convert $file $file.jpg
rm $file
done
for file in `find . -type f -iname "*.jpg"`;
do
echo "Resizing $file"
# Remove Metadata orientation and rotate image
# since photoframe doesn't respect this setting
EXIFORIENT=$(identify -format "%[exif:*orientation*]" $file | head -n 1 | cut -d"=" -f2)
if [[ $EXIFORIENT -gt 4 ]] ;
then
#Remove all meta data
mogrify -strip $file
if [[ $EXIFORIENT -eq 6 ]];
then
#RightTop -> rotate right
mogrify -rotate 90 $file
fi
if [[ $EXIFORIENT -eq 8 ]];
then
#LeftBottom -> rotate left
mogrify -rotate -90 $file
fi
fi
IMAGEDIM=$(identify -format "%g" $file | cut -d"+" -f1)
WIDTH=`echo $IMAGEDIM | cut -d"x" -f1`
HEIGHT=`echo $IMAGEDIM | cut -d"x" -f2`
echo " Original size: ${WIDTH}x${HEIGHT}"
if [[ $WIDTH -gt $HEIGHT ]] ;
then
echo " Resizing $IMAGEDIM to width 1024"
mogrify -resize $SCREENWIDTH $file
IMAGEDIM=`identify -verbose $file | grep "Geometry" | cut -d" " -f4 | cut -d"+" -f1`
HEIGHT=`echo $IMAGEDIM | cut -d"x" -f2`
XOFFSET=0
YOFFSET=$[ ($HEIGHT - $SCREENHEIGHT) / 2 ]
if [[ ${YOFFSET} -lt 0 ]] ;
then
YOFFSET=$[ ${YOFFSET} * (-1) ]
fi
echo " Cropping to ${SCREENWIDTH}x${SCREENHEIGHT}";
mogrify -crop ${SCREENWIDTH}x${SCREENHEIGHT}+${XOFFSET}+${YOFFSET} $file
else
echo " Resizing $IMAGEDIM to height 600"
mogrify -resize x${SCREENHEIGHT} $file
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment