Skip to content

Commit

Permalink
Resize images to the size of the background image
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias K committed Jan 3, 2020
1 parent b3927ec commit 6a5b595
Showing 1 changed file with 56 additions and 33 deletions.
89 changes: 56 additions & 33 deletions dalliclick.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,27 @@ GRAYMASKDEFAULTFILENAME="tmp_maskimage.png"
# generate a background image?
GENERATE_BACKGROUND=1
BACKGROUNDIMAGENAME="tmp_background.png"
RESIZEIMAGENAME="tmp_resized.png"
FILLUPCOLOR="black"

# functions
function usage {
echo "This script creates partially occluded image versions of the input image"
echo "Usage: $SCRIPTNAME <options> imagefiles" >&2
echo "-h (this) help"
echo "-v Increase verbosity (more details on actions)"
echo "-n number of segments, max. 255"
echo "-o output image base name (out -> out_NNN.png)"
echo "-b background (canvas) image"
echo "-m maskimage (with label gray values 1,2,3,...)"
echo "-t do not generate temporary directory (working dir is used)"
echo "-O format of output files (png)"
echo "Usage: $SCRIPTNAME <Options> imagefile..."
echo "Options:"
echo "-h Show this help"
echo "-v Increase verbosity (more details on actions)"
echo "-n Segments Number of segments, max. 255 (Default 10)"
echo "-o Name Output image base name (out -> out_NNN.png)"
echo "-b File Background (canvas) image. The output images will have exactly"
echo " the size of the background image."
echo " If this is not specified, an artifical background image will be"
echo " generated."
echo "-B color Color for filling up images in wrong ratio (only relevant with -b)"
echo "-m maskimage User specific mask image with label gray values 1,2,3,..."
echo " If this is not specified a random mask image will be generated."
echo "-t Do not generate temporary directory (working dir is used)"
echo "-O format Format of output files (Default: png)"
[[ $# -eq 1 ]] && exit $1 || exit $EXIT_FAILURE
}

Expand Down Expand Up @@ -131,27 +139,25 @@ function dallicleanup(){
# cleanup *generated* files
if [ $GENERATE_BACKGROUND -ne 0 ] ; then
# # remove 'starting' canvas
if [ -f "$BACKGROUNDIMAGE" ] ; then
rm "$BACKGROUNDIMAGE"
fi
rm -f "$BACKGROUNDIMAGE"
fi
#if [ $VERBOSE -ne 0 ] ; then echo "cleaning up mask"; fi
if [ $GENERATE_MASK -ne 0 ] ; then
# # remove mask image
if [ -f "$TMPMASKIMAGENAME" ] ; then
rm "$TMPMASKIMAGENAME"
fi
rm -f "$TMPMASKIMAGENAME"
fi
if [ -f "$GRAYMASKFILE" ] ; then
rm "$GRAYMASKFILE"
if [ "$INPUTIMAGERESIZED" ]; then
rm -f "$INPUTIMAGERESIZED"
fi
rm -f "$GRAYMASKFILE"
# cleanup the temporary directory, if that was created
#if [ $VERBOSE -ne 0 ] ; then echo "cleaning up tmpdir"; fi
if [ $GENTMPDIR -eq 0 ] ; then
if [ -d "$TMPDIR" ] ; then
rm -rf "$TMPDIR"
fi
fi

}

function failbail(){
Expand All @@ -176,7 +182,7 @@ fi
# parse options
# Option -h (help) should always be there
# if you have an option argument to be parsed use ':' after option
while getopts ':n:i:b:m:o:O:vht' OPTION ; do
while getopts ':n:i:b:m:o:O:B:vht' OPTION ; do
case $OPTION in
v) VERBOSE=$((VERBOSE+1))
;;
Expand All @@ -196,6 +202,8 @@ while getopts ':n:i:b:m:o:O:vht' OPTION ; do
b) BACKGROUNDIMAGENAME="$OPTARG"
GENERATE_BACKGROUND=0
;;
B) FILLUPCOLOR="$OPTARG"
;;
t) GENTMPDIR=1
;;
\?) echo "Unknown option \"-$OPTARG\"." >&2
Expand Down Expand Up @@ -230,10 +238,35 @@ for INPUTIMAGE in "$@"
do
if [ $VERBOSE -ne 0 ] ; then echo "processing $INPUTIMAGE"; fi

WIDTH=`identify -format "%w" "$INPUTIMAGE"`
HEIGHT=`identify -format "%h" "$INPUTIMAGE"`
if [ -z "$WIDTH" -o -z "$HEIGHT" ] ; then failbail "cannot identify size of image $INPUTIMAGE" ; fi
if [ $VERBOSE -ne 0 ] ; then echo "[debug] imagesize: $IMGSIZE $WIDTH $HEIGHT"; fi
if [ $GENERATE_BACKGROUND -ne 0 ] ; then
WIDTH=`identify -format "%w" "$INPUTIMAGE"`
HEIGHT=`identify -format "%h" "$INPUTIMAGE"`
if [ -z "$WIDTH" -o -z "$HEIGHT" ] ; then
failbail "cannot identify size of image $INPUTIMAGE" ;
fi
# do this in the temporary directory
BACKGROUNDIMAGE="$TMPDIR/$BACKGROUNDIMAGENAME"
# generate 'starting' canvas
CMD="$CONVERT_BIN -size ${WIDTH}x$HEIGHT plasma:fractal $BACKGROUNDIMAGE"
if [ $VERBOSE -ne 0 ] ; then echo $CMD; fi
eval $CMD
IMAGETOUSE=$INPUTIMAGE
else
BACKGROUNDIMAGE="$BACKGROUNDIMAGENAME"
WIDTH=`identify -format "%w" "$BACKGROUNDIMAGE"`
HEIGHT=`identify -format "%h" "$BACKGROUNDIMAGE"`
if [ -z "$WIDTH" -o -z "$HEIGHT" ] ; then
failbail "cannot identify size of image $BACKGROUNDIMAGE" ;
fi
INPUTIMAGERESIZED="$TMPDIR/$RESIZEIMAGENAME"
CMD="$CONVERT_BIN \"$INPUTIMAGE\" -auto-orient -resize ${WIDTH}x$HEIGHT \
-background $FILLUPCOLOR -compose Copy -gravity center -extent ${WIDTH}x$HEIGHT \"$INPUTIMAGERESIZED\""
if [ $VERBOSE -ne 0 ] ; then echo $CMD; fi
eval $CMD
IMAGETOUSE=$INPUTIMAGERESIZED
fi

if [ $VERBOSE -ne 0 ] ; then echo "[debug] imagesize: $WIDTH $HEIGHT"; fi

# generate a mask image
if [ $GENERATE_MASK -ne 0 ] ; then
Expand All @@ -246,16 +279,6 @@ do
# debug: convert labelled mask image to 256 colors
# convert -colors 256 -colorspace GRAY $MASKIMAGENAME ?

if [ $GENERATE_BACKGROUND -ne 0 ] ; then
# do this in the temporary directory
BACKGROUNDIMAGE="$TMPDIR/$BACKGROUNDIMAGENAME"
# generate 'starting' canvas
CMD="$CONVERT_BIN -size ${WIDTH}x$HEIGHT plasma:fractal $BACKGROUNDIMAGE"
if [ $VERBOSE -ne 0 ] ; then echo $CMD; fi
eval $CMD
else
BACKGROUNDIMAGE="$BACKGROUNDIMAGENAME"
fi

# output name
if [ $RESULTIMAGEBASE_GIVEN -ne 0 ]
Expand All @@ -279,7 +302,7 @@ do
eval $CMD

# mask the image
CMD="$COMPOSITE_BIN \"$BACKGROUNDIMAGE\" \"$INPUTIMAGE\" \"$GRAYMASKFILE\" \"$RESULTIMAGE\""
CMD="$COMPOSITE_BIN \"$BACKGROUNDIMAGE\" \"$IMAGETOUSE\" \"$GRAYMASKFILE\" \"$RESULTIMAGE\""
if [ $VERBOSE -ne 0 ]; then echo "$CMD" ; fi
eval $CMD
if [ $VERBOSE -ne 0 ]; then echo "result image written to "$RESULTIMAGE ; fi
Expand Down

0 comments on commit 6a5b595

Please sign in to comment.