#!/bin/bash
# usage: kill_image ITERATIONS IMAGE
# converts IMAGE to a mpeg and back to the original format ITERATIONS times,
# adding a numeric suffix to each step
# coded by e36freak
# MIT License
# assign options to vars after checking validity
if (($# < 2)); then
printf '%s\n' 'not enough options' >&2
exit 1
fi
if [[ -z $1 || $1 = *[!0-9]* ]]; then
printf 'invalid number for ITERATIONS: %s\n' "$1" >&2
exit 1
fi
times=$1
if [[ ! -f $2 || ! -r $2 ]]; then
printf '%s: permission denied\n' >&2
exit 1
fi
image=$2
# get prefix and extension
pref=${image%.*}
ext=${image##*.}
# determine how many zeroes to pad
len=${#times}
# store previously generated image
prev=$image
# loop ITERATIONS times
for ((i=1; i<=times; i++)); do
# 0-pad suffix
printf -v suff '%0*d' "$len" "$i"
# convert from original format to mpeg
convert "$prev" "$pref.mpeg" &&
# convert back to original extension, adding suffix
convert "$pref.mpeg" "$pref$suff.$ext" || continue
# remove mpeg file, not needed
rm "$pref.mpeg"
prev=$pref$suff.$ext
done