Skip to content

Instantly share code, notes, and snippets.

@jbristow

jbristow/degrade.sh

Created Oct 31, 2013
Embed
What would you like to do?
A strange glitch experiment
roll()
{
# echo "convert ${1} -roll +${2}+${3} $1"
convert ${1} -roll +${2}+${3} $1
}
roll_h()
{
roll $1 $((($RANDOM+1) % $2)) 0
}
roll_v()
{
roll $1 0 $((($RANDOM+1) % $2))
}
roll_b()
{
roll $1 $((($RANDOM+1) % $2)) $((($RANDOM+1) % $3))
}
resize()
{
# echo "convert $1 -alpha set \( +clone -background $2 $3 \) -gravity center -compose Src -composite $1"
convert $1 -alpha set \( +clone -background $2 $3 \) -gravity center -compose Src -composite $1
}
rotate()
{
resize $1 $3 "-rotate $2"
}
skew_h()
{
skew $1 $2 0 $3
}
skew_v()
{
skew $1 0 $2 $3
}
skew()
{
resize $1 $4 "-shear ${2}x${3}"
}
FILE=$(basename "$1")
FILENAME="${FILE%.*}"
EXTENSION=${FILE##*.}
DEGRADED_FILE="${FILENAME}_degraded.${EXTENSION}"
WIDTH=$(identify -format "%[fx:w]" $FILE)
HEIGHT=$(identify -format "%[fx:h]" $FILE)
GEO="$WIDTHx$HEIGHT"
TEMP_FILENAME="${FILENAME}-temp"
cp $FILE $DEGRADED_FILE
#echo convert $DEGRADED_FILE -resize 1x1 ${TEMP_FILENAME}-color.${EXTENSION}
convert $DEGRADED_FILE -resize 1x1 ${TEMP_FILENAME}-color.${EXTENSION}
COLOR=$(convert ${TEMP_FILENAME}-color.${EXTENSION} -format '%[pixel:p{1,1}]' info:-)
MAX=1000
DELTA=$(python -c "print 40.0/$MAX")
for VARIABLE in $(seq 1 $MAX);
do
PREV="${FILENAME}_degraded_prev.jpg"
cp $DEGRADED_FILE $PREV
# echo "# Degradation $VARIABLE"
QUAL=$(python -c "from math import floor; print 81 - long(floor($DELTA * $VARIABLE))")
# echo "convert $DEGRADED_FILE -quality $QUAL $DEGRADED_FILE"
convert $DEGRADED_FILE -quality $QUAL $DEGRADED_FILE
CHOICE=$(($RANDOM % 10))
if [ $CHOICE == 0 ]; then
# roll_h $DEGRADED_FILE $WIDTH
# elif [ $CHOICE == 1 ]; then
# roll_v $DEGRADED_FILE $HEIGHT
# elif [ $CHOICE == 2 ]; then
roll_b $DEGRADED_FILE $HEIGHT $WIDTH
elif [ $CHOICE == 1 ]; then
rotate $DEGRADED_FILE $((($RANDOM+1) % 360)) $COLOR
# elif [ $CHOICE == 5 ]; then
# rotate $DEGRADED_FILE $((-1 * ($RANDOM+1) % 360)) $COLOR
# elif [ $CHOICE == 6 ]; then
# skew_h $DEGRADED_FILE $((($RANDOM+1) % $WIDTH)) $COLOR
# elif [ $CHOICE == 7 ]; then
# skew_v $DEGRADED_FILE $((($RANDOM+1) % $HEIGHT)) $COLOR
# elif [ $CHOICE == 8 ]; then
# skew $DEGRADED_FILE $((($RANDOM+1) % $WIDTH)) $((($RANDOM+1) % $HEIGHT)) $COLOR
elif [ $CHOICE == 2 ]; then
BLEND=$(($RANDOM % 100))
# echo "composite -blend $BLEND $FILE $DEGRADED_FILE $DEGRADED_FILE"
composite -blend $BLEND $FILE $DEGRADED_FILE $DEGRADED_FILE
fi
BLEND=$(($RANDOM % 100))
# echo "composite -blend $BLEND $PREV $DEGRADED_FILE $DEGRADED_FILE"
composite -blend $BLEND $PREV $DEGRADED_FILE $DEGRADED_FILE
# BLEND=$(($RANDOM % 50))
# echo "composite -blend $BLEND $FILE $DEGRADED_FILE $DEGRADED_FILE"
# composite -blend $BLEND $FILE $DEGRADED_FILE $DEGRADED_FILE
done
#echo "composite -blend 50 $FILE $DEGRADED_FILE ${FILENAME}_degraded_final.${EXTENSION}"
composite -blend 50 $FILE $DEGRADED_FILE ${FILENAME}_degraded_final.${EXTENSION}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.