ImageMagick: Bidirectional repeated liquid-rescale (content aware scaling)



The use of heavy seam carving (liquid rescale/content aware scaling) in images and video has been well expressed on the internet over the past 10 years¹. The concept of repeated "bidirectional" seam carving has been demonstrated here numerous times in the past².

The concept of bidirectional carving is to resize the image only slightly, and return it to its original dimensions. If this is done repeatedly many times (hundreds or thousands of iterations) the image will continue to corrupt and evolve in novel ways. A simple bash script used for automating the process is presented below.
#!/bin/bash

# Repeated bidirectional 'seam carving' on image. (Requires ImageMagick).
#  - Arguments: filename, iterations, size difference, quality, milestones.
#  - See 'NOTES' at bottom of script for further details
# ver: 2017.11.15.13.07.17
# source: https://oioiiooixiii.blogspot.com

function main()
{   
   # Make duplicate file for working on
   [ "$4" == "png" ] \
      && ext="png" \
      && quality="" \
      || quality="-format jpg -quality $4"
   filename="${1}_lqr-i$2-s$3-q$4.${ext:-jpg}"
   convert "$1" $quality "$filename"
   
   # Set up scaling variables
   originalRes="$(identify $1 | cut -d' ' -f3)"
   pix="$3"
   altRes="$(( $(cut -dx -f1 <<<$originalRes)+pix ))x\
           $(( $(cut -dx -f2 <<<$originalRes)+pix ))"

   #main loop
   for ((i=0;i<"$2";i++)) 
   {
      clear
      printf "FILE: $filename\nFRAMES: $((frame))\nITERATION: $((i+1))\n"
      printf "* Scaling to alt. resolution '${altRes//[[:space:]]/}'\n"
      mogrify -liquid-rescale "$altRes!" $quality "$filename"
      printf "* Scaling to original resolution '$originalRes'\n"
      mogrify -liquid-rescale "$originalRes!" $quality "$filename" 
      
      # Create a new image at milestone interval, if set
      [ ! -z "$5" ] && ! (( $i % $5 )) \
         && cp "$filename" "$((frame++))_$filename"
   }
}

main "$@"
exit

### NOTES ######################################################################
# $1=filename - Name/location of image.
# $2=iterations - The total number of desired resizes.
# $3=size difference - Amount of pixels to scale by (positive or negative).
# $4=quality - Set desired jpeg quality or 'png' (compression causes entropy).
# $5=milestones - Create new file at specified interval, capturing current state
# Possible script improvement: File i/o location in ram drive /dev/shm/ etc.
download: imageMagick_bidirectional-seam-carve.sh



The process can develop numerous types of effect, depending on the attributes given. Shapes can become angular, or rudimentary. Sections of the image can begin to develop seams that tear and germinate. Eventually, most images cascade down into a mess of chaos that never resolves.

The example above demonstrates some of the effects different arguments used in the script can have on an image, though it is in no way exhaustive, nor shows the extremities of the effect. For a more extreme example, see the video below. The starting image size was 960x408, and the arguments given at run-time were: 10,000 iterations, size reduction of 100 pixels, jpeg quality of 90, and every frame saved.


¹ info: http://knowyourmeme.com/memes/content-aware-scaling
² related: https://oioiiooixiii.blogspot.com/search/label/Seam%20Carving
source video: https://en.wikipedia.org/wiki/The_Doctor_and_the_Devils