#!/bin/sh # # de-pixelate [-limit N] input_image output_image # # Try to find and remove any row/columns in the image that are duplicates of # the previous row/column. That is remove the extra pixelating rows and # columns. # # Option # -limit N Merge at most N rows/columns into a single row/column # Can be used to prevent a naturally occuring duplicate # row/columns from being merged into a single line/column # For example -limit 1 no-ops this whole script! # N should be equal to or slightly large that the 'pixel' size. # PROGNAME=`type $0 | awk '{print $3}'` # search for executable on path PROGDIR=`dirname $PROGNAME` # extract directory of program PROGNAME=`basename $PROGNAME` # base name of program tmpdir=/tmp/$PROGNAME.$$ mkdir $tmpdir if [ $? -ne 0 ]; then echo >&2 "$PROGNAME: Failed to create directory \"$tmpdir\" -- ABORTING" exit 1 fi trap "rm -rf $tmpdir; exit 0" 0 trap "rm -rf $tmpdir; exit 1" 1 2 3 15 # Get limit option limit=0 # limit will never be reached if [ "x$1" = "x-limit" ]; then shift; limit=$1; shift fi # Read input image, once only as it may be pipelines, and save a MPC copy of # each individual row of pixels as temporary files. convert "$1" -crop 0x1 +repage $tmpdir/rows_%06d.mpc # remove duplicate rows (by removing its .mpc index file) last="" for pic in $tmpdir/rows_*.mpc; do count=`expr $count + 1` if [ "$last" ] && [ $count -ne $limit ] && [ `compare -metric AE $pic $last null: 2>&1` -eq 0 ]; then rm $pic else last=$pic count=0 fi done # Rejoin and re-split image into columns convert $tmpdir/rows_*.mpc -append -crop 1x0 +repage $tmpdir/cols_%06d.mpc # lets save some disk space as we are finished with the row images rm -f $tmpdir/rows_*.mpc $tmpdir/rows_*.cache # remove duplicate columns last="" count=0 for pic in $tmpdir/cols_*.mpc; do count=`expr $count + 1` if [ "$last" ] && [ $count -ne $limit ] && [ `compare -metric AE $pic $last null: 2>&1` -eq 0 ]; then rm $pic else last=$pic count=0 fi done # Rejoin the columns and return the de-pixelated image... convert $tmpdir/cols_*.mpc +append "$2" # not realy needed, but lets clean up anyway rm -f $tmpdir/cols_*.mpc $tmpdir/cols_*.cache rm -rf $tmpdir