Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Last active January 2, 2016 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eusonlito/8298892 to your computer and use it in GitHub Desktop.
Save eusonlito/8298892 to your computer and use it in GitHub Desktop.
Bulk pictures processing to fit it into a canvas
#!/bin/bash
# Put this file into a folder with PNG pictures with transparent background
# ----- Default values ----- #
ext='.png' # Original pictures have this extension
prefix='frame-' # Final pictures name with this prefix
background='white' # Final pictures background color
padding=100 # Do not fit to max width/height, leave some padding
quality=80 # Final pictures quality
fps=10 # Frames/second speed animation
size='480x800' # Final pictures size
folder='bootanimation' # Folder to store final bootanimation
shuffle='true' # Shuffle files before process
debug='true' # Enable/Disable process messages
verbose='true' # Enable/Disable file processing status
mix='true' # Create a mixed photo with each transition
blur='0x20+0' # For mixed transitions, set the blur effect
max=100 # Max numbers of pictures to use (before MIX process)
gif=5 # Create a final demo gif with X*10 fames (0 to disable and 9 as max)
start='' # Empty to wait start confirmation, y to start without confirmation
# TIPS:
# To get how many pictures that you need to create an animation without repeated pictures,
# check boot time and divide by FPS.
# If you are using MIX option, the you will get the double of original pictures
# Example: boot time is about 22 seconds and fps 10. You need 220 pictures to create
# an animation without repeated pictures
# If you have enabled MIX, you will need 110 pictures because mixed photos will be
# generated automatically and all together are 220 frames
# You can have more pictures that you need in folder and set MAX value to limit pictures
# to use (before MIX process)
# ----- Do not edit from here ----- #
while true; do
case $1 in
--start )
start="$2"
shift 2
;;
--ext )
ext=".$2"
shift 2
;;
--prefix )
prefix="$2"
shift 2
;;
--background )
background="$2"
shift 2
;;
--padding )
if [ "`echo $2 | grep -v "^[0-9]\+$"`" != '' ]; then
echo ''
echo '--padding value is not valid. Only accept numbers from 1 to 999.'
echo ''
exit 2
fi
padding="$2"
shift 2
;;
--quality )
if [ "`echo $2 | grep -v "^[0-9]\+$"`" != '' ]; then
echo ''
echo '--quality value is not valid. Only accept numbers from 1 to 999.'
echo ''
exit 2
fi
quality="$2"
shift 2
;;
--fps )
if [ "`echo $2 | grep -v "^[0-9]\+$"`" != '' ]; then
echo ''
echo '--fps value is not valid. Only accept numbers from 1 to 999.'
echo ''
exit 2
fi
fps="$2"
shift 2
;;
--folder )
folder="$2"
shift 2
;;
--size )
if [ "`echo $2 | grep -v "^[0-9]\+x[0-9]\+$"`" != '' ]; then
echo ''
echo '--size value is not valid. Value format must be like 480x800.'
echo ''
exit 2
fi
size="$2"
shift 2
;;
--shuffle )
if [ "$2" == 'true' ]; then
shuffle='true'
else
shuffle='false'
fi
shift 2
;;
--debug )
if [ "$2" == 'true' ]; then
debug='true'
else
debug='false'
fi
shift 2
;;
--verbose )
if [ "$2" == 'true' ]; then
verbose='true'
else
verbose='false'
fi
shift 2
;;
--mix )
if [ "$2" == 'true' ]; then
mix='true'
else
mix='false'
fi
shift 2
;;
--max )
if [ "`echo $2 | grep -v "^[0-9]\+$"`" != '' ]; then
echo ''
echo '--max value is not valid. Only accept numbers from 1 to 999.'
echo ''
exit 2
fi
max="$2"
shift 2
;;
--gif )
if [ "`echo $2 | grep -v "^[0-9]$"`" != '' ]; then
echo ''
echo '--gif value is not valid. Only accept numbers from 0 to 9.'
echo ''
exit 2
fi
gif="$2"
shift 2
;;
-h | --help )
echo ''
echo 'Usage: sh '$(basename $0)
echo ''
echo 'Options with default value:'
echo ''
echo '--ext .png Original pictures have this extension'
echo '--prefix frame- Final pictures name with this prefix'
echo '--background white Final pictures background color'
echo '--padding 100 Do not fit to max width/height, leave some padding'
echo '--quality 80 Final pictures quality'
echo '--fps 10 Frames/second speed animation'
echo '--size 480x800 Final pictures size'
echo '--folder bootanimation Folder to store final bootanimation'
echo '--shuffle true Shuffle files before process'
echo '--debug true Enable/Disable process messages'
echo '--verbose true Enable/Disable file processing status'
echo '--mix true Create a mixed photo with each transition'
echo '--blur 0x20+0 For mixed transitions, set the blur effect'
echo '--max 100 Max numbers of pictures to use (before MIX process)'
echo '--gif 5 Create a final demo gif with X*10 fames (0 to disable and 9 as max)'
echo '--start y Empty to wait start confirmation, y to start without confirmation'
echo ''
exit 2
;;
* )
if [ "$1" != '' ]; then
echo ''
echo "No valid paramenter "$1
./$(basename $0) --help
exit 2
fi
break
;;
esac
done
folder=$folder'-'$size
echo ''
echo 'Settings:'
echo '----------------------------------'
echo ''
echo ext=$ext
echo prefix=$prefix
echo background=$background
echo padding=$padding
echo quality=$quality
echo fps=$fps
echo size=$size
echo folder=$folder
echo shuffle=$shuffle
echo debug=$debug
echo verbose=$verbose
echo mix=$mix
echo blur=$blur
echo max=$max
echo gif=$gif
echo start=$start
echo ''
while [ "$start" == '' ]; do
read -p 'Start with process (Y/n)? ' start
done
if [ "$start" != 'y' ] && [ "$start" != 'Y' ]; then
echo ''
echo 'Process aborted'
echo ''
exit 1
fi
echo_debug () {
if [ "$debug" == 'true' ]; then
echo ''
echo $1
echo ''
fi
}
echo_verbose () {
if [ "$verbose" == 'true' ]; then
echo $1
echo ''
fi
}
total=`ls *$ext 2> /dev/null | wc -l`
if [ "$total" == '0' ]; then
echo ''
echo 'There are not pictures to process'
echo ''
exit 1
fi
if [ "`find . -name "* *$ext"`" != '' ]; then
echo ''
echo 'There are some pictures with spaces in name'
read -p 'Do you want that rename it automatically? [Y/n] ' rename
if [ "$rename" != 'y' ] && [ "$rename" != 'Y' ] && [ "$rename" != '' ]; then
echo ''
echo 'Process aborted'
echo ''
exit 1
fi
echo_debug 'Fixing spaces in filenames'
find . -name "* *$ext" | rename 's/ /_/g'
fi
if [ "$mix" == 'true' ]; then
step=2
else
step=1
fi
echo_debug 'Clearing previous folders'
rm -rf $folder tmp
mkdir tmp
if [ "$max" -gt '0' ] && [ "$total" -gt "$max" ]; then
echo_debug 'There are '$total' pictures in folder but they will be limited to '$max
total="$max"
fi
echo_debug 'Resizing '$total' pictures'
cd tmp
convert -quiet -size $size xc:none canvas.gif > /dev/null 2> /dev/null
if [ "$shuffle" == 'true' ]; then
files=`ls ../*$ext | sort -R`
else
files=`ls ../*$ext`
fi
num=0
index=0
for i in ${files[@]}; do
index=$(($index + 1))
if [ "$max" -gt '0' ] && [ "$index" -gt "$max" ]; then
break
fi
echo_verbose 'Resizing picture '$index' of '$total
new=$prefix$(printf '%03d' $num)'.png'
cp "$i" "$new"
convert -quiet "$new" -background none -bordercolor none -trim -border $padding'x'$padding +repage "$new" > /dev/null 2> /dev/null
convert -quiet "$new" -resize $size "$new" > /dev/null 2> /dev/null
convert -quiet -gravity center canvas.gif "$new" -compose Overlay -composite "$new" > /dev/null 2> /dev/null
num=$(($num + $step))
done
if [ "$mix" == 'true' ]; then
echo_debug 'Creating mix pictures'
num=0
index=0
for i in $prefix*'.png'; do
index=$(($index + 1))
echo_verbose 'Mixing picture '$index' of '$total
mixed=$prefix$(printf '%03d' $(($num + 1)))'.png'
next=$prefix$(printf '%03d' $(($num + 2)))'.png'
if [ ! -f "$next" ]; then
next=$prefix'000.png'
fi
convert -quiet "$next" -background transparent -alpha set -channel A -evaluate Divide 1.5 "$mixed" > /dev/null 2> /dev/null
composite -quiet -background transparent -gravity center "$mixed" "$i" "$mixed" > /dev/null 2> /dev/null
mogrify -quiet -background $background -flatten "$i" > /dev/null 2> /dev/null
mogrify -quiet -background $background -flatten "$mixed" > /dev/null 2> /dev/null
convert -quiet "$mixed" -motion-blur "$blur" "$mixed" > /dev/null 2> /dev/null
num=$(($num + 2))
done
echo_debug 'Flatten images with '$background' background and quality at '$quality'%'
mogrify -quiet -format jpg -quality $quality $prefix*'.png' > /dev/null 2> /dev/null
else
echo_debug 'Flatten images with '$background' background and quality at '$quality'%'
mogrify -quiet -background $background -flatten -format jpg -quality $quality $prefix*'.png' > /dev/null 2> /dev/null
fi
cd ..
echo_debug 'Creating bootanimation folder'
install -d $folder/part0
mv tmp/$prefix*.jpg $folder/part0
cd $folder
echo $size' '$fps | sed 's/x/ /' > desc.txt
echo '' >> desc.txt
echo 'p 0 0 part0' >> desc.txt
echo '' >> desc.txt
echo_debug 'Creating bootanimation zip'
zip -q0 ${PWD##*/}'-'$size'-bootanimation.zip' desc.txt part0/*
if [ "$gif" -gt '0' ]; then
echo_debug 'Creating bootanimation demo gif'
convert -adaptive-resize 480x853 -delay 10 -loop 0 part0/$prefix'0'[0-$gif]*.jpg bootanimation.gif
fi
cd ..
rm -rf tmp
echo_debug 'Done!'
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment