-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathde-pixelate
83 lines (73 loc) · 2.64 KB
/
de-pixelate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/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 so as to reduce enlarged 'pixel boxes' down to a single pixel.
#
# 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 using "-limit 1" will no-op this whole script!
# N should be equal to or slightly larger that the 'pixel'
# size that is present in the input image.
#
# NOTE: If the actual number of 'pixel boxes' is known, as faster solution
# is to simply resize the image using "-sample". However if the size is
# unknown, then you are better off using this script.
#
####
#
# Anthony Thyssen 2007
#
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
umask 77
tmp=`mktemp -d "${TMPDIR:-/tmp}/$PROGNAME.XXXXXXXXXX"` ||
{ echo >&2 "$PROGNAME: Unable to create temporary file"; exit 10;}
trap 'rm -rf "$tmp"' 0
trap 'exit 2' 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.
magick "$1" -crop 0x1 +repage "$tmp/rows_%06d.mpc"
# remove duplicate rows (by removing its .mpc index file)
last=""
for pic in $tmp/rows_*.mpc; do
count=`expr $count + 1`
if [ "$last" ] && [ $count -ne $limit ] &&
[ `magick compare -dissimilarity-threshold 100% \
-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
magick $tmp/rows_*.mpc -append -crop 1x0 +repage $tmp/cols_%06d.mpc
# lets save some disk space as we are finished with the row images
rm -f $tmp/rows_*.mpc $tmp/rows_*.cache
# remove duplicate columns
last="" count=0
for pic in $tmp/cols_*.mpc; do
count=`expr $count + 1`
if [ "$last" ] && [ $count -ne $limit ] &&
[ `magick compare -dissimilarity-threshold 100% \
-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...
magick $tmp/cols_*.mpc +append "$2"
# not realy needed, but lets clean up anyway
rm -f $tmp/cols_*.mpc $tmp/cols_*.cache
rm -rf $tmp