lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: png cropping


From: Patrick Horgan
Subject: Re: png cropping
Date: Thu, 18 Sep 2008 19:45:10 -0700
User-agent: Thunderbird 2.0.0.16 (X11/20080724)

Jonathan Kulp wrote:
Hi Patrick,

I've been running your script trying to use the command-line arguments, and something's happening with the format argument. I specify it with an argument, but then I still get prompted for format. I might not be doing the flag right. I've tried it with -f=jpeg, -f=JPEG, -fJPEG, and I think that's it. I'll copy the terminal output below so you can see. On this example I use -fGIF and while it doesn't make the script fail, it does still ask me for a format. Am I specifying the format wrong?
it would have to be the same as the input from the command line, i.e. -f=jpeg or -f=gif---I don't have a clue why it would fail, though with our weird crossing of versions I don't know which version you're using. I put a version in this one, and if you specify -v it will tell you the version and quit. I fixed the defaults as you asked, and put in defaults for N 72 and png, (and if they pick transparency a default of png as well). If the command line args still don't work please let me know. They work here. My experience is that whenever I'm sure I've tested everything there are still many bugs, so please let me know:)
BTW, in the script I attached to the last email, you'll notice that with transparent background you have a choice between either png or gif--it's not forced to png. Is there a way to work this option into your version?
Yep, fixed.

Patrick
#!/bin/bash
#*****************************************************#
# Script for making image files from lilypond source  #
# suitable for use as musical examples to insert in a #
# document or web page.                               #
#*****************************************************#

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# setformatlist - get's the list of all the things that
# you can convert to
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
setformatlist()
{
    currentdir=`pwd`
    examp=`which ppmtojpeg`
    if [ $? -eq 0 ] ; then
        OUTDIR="`dirname $examp`"
        cd $OUTDIR
        ppmtos=`ls ppmto* | sed -n s/ppmto//p`
        pnmtos=`ls pnmto* | sed -n s/pnmto//p`
        alltos=`echo  $ppmtos $pnmtos | tr " " "\n" | sort -u | tr "\n" " "`
    fi
    cd $currentdir
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# usage is called when we're called incorrectly.
# it never returns
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
usage()
{
    echo "Usage: " `basename $0` " [-v] [-t] [-rN] [-fFORMAT] filename"
    echo "      -v              print version number and quit"
    echo "      -t              indicates transparency is desired"
    echo "      -r=N            set resolution to N (usually 72-2000)"
    echo "      -f=FORMAT       set format to FORMAT one of:"
    echo "                        jpeg, png, tiff, gif, pcx, bmp"
    echo "      filename        a lilypond file"
    exit -1
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Set prompt to the prompt you want to give a user
# goodvals to the list of acceptable values
# call getval
# when it returns your value is in outval
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
getval()
{
    flag="notdone"
    address@hidden
    until [ $flag == "done" ] ; do
        echo -n $prompt " "
        read inval
        if [ A$inval == "A" ] ; then
            if [ A$default != 'A' ] ; then
                inval=$default
                default=""
            else
                echo You must enter a value
                index=0
                echo -n "Expecting one of : "
                while [ "$index" -lt "$elementcount" ] ; do
                    echo -n "${goodvals["$index"]}" " "
                    let index++
                done
                echo
            fi
        fi
        if [ A$inval != "A" ] ; then
            index=0
            while [ "$index" -lt "$elementcount" ] ; do
                if [ ${goodvals[$index]} == $inval ] ; then
                    flag="done"
                    outval=${goodvals[$index]}
                fi
                let index++
            done
            if [ $flag != "done" ] ; then
                index=0
                echo -n "Expecting one of : "
                while [ "$index" -lt "$elementcount" ] ; do
                    echo -n "${goodvals["$index"]}" " "
                    let index++
                done
                echo
            fi
        fi
    done
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Set prompt to the prompt you want to give a user
# call getnumval
# when it returns your value is in outval
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
getnumval()
{
    flag="notdone"
    until [ $flag == "done" ] ; do
        echo -n $prompt " "
        read inval
        if [ A$inval == "A" ] ; then
            if [ A$default != 'A' ] ; then
                inval=$default
                default=""
            else
                echo You must enter a value, expecting a positive numeric value
            fi
        fi
        if [ "A"$inval != 'A' ] ; then
            case $inval in
                *[^0-9]*) echo "Error: expecting positive numeric value" ;;
                * ) flag="done" ;;
            esac
        fi
    done
    outval=$inval
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# validatearg()
# set inarg to the 
# set goodvals to the list of acceptable values
# set prompt to the error message you'd like to give,
# for example "ERROR: bad value for transparency arg"
# this routine will append to it, " expecting: " and
# the list of values from goodvals, then call usage
# to exit
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

validatearg()
{
    flag="notgood"
    address@hidden
    index=0
    if [ "A"$inarg != "A" ] ; then
        while [ "$index" -lt "$elementcount" ] ; do
            if [ ${goodvals[$index]} == $inarg ] ; then
                flag="good"
                outval=${goodvals[$index]}
            fi
            let index++
        done
    fi
    if [ $flag != "good" ] ; then
        index=0
        echo -n $prompt
        echo -n " expecting one of : "
        while [ "$index" -lt "$elementcount" ] ; do
            echo -n "${goodvals["$index"]}" " "
            let index++
        done
        echo
        usage
    fi
}

getopt_simple()
{
    until [ -z "$1" ] ; do
        if [ ${1:0:1} = '-' ] ; then
            tmp=${1:1}               # Strip off leading '-' . . .
            if [ ${tmp:0:1} = '-' ] ; then
                tmp=${tmp:1}         # Allow double -
            fi
            parameter=${tmp%%=*}     # Extract name.
            value=${tmp##*=}         # Extract value.
            eval $parameter=$value
        else
            filename=$1
        fi
        shift
    done
}

alltos=""
transparency='no'
t='no'
resolution=0
r=0
format='none'
f='none'
filename="none"
version=1.0

setformatlist
if [ $? -ne 0 ] ; then
    echo "Sorry, you have to have the netpbm utilities installed to use this."
    exit 0
fi

getopt_simple $*

if [ "$v" == 'v' ] ; then
    echo `basename $0` version $version
    exit
fi

if [ $filename == "none" ] ; then
    usage
fi

if [ $t != 'no' ] ; then
    transparency=$t
fi

case $r in
        *[^0-9]*) echo "Error: resolution not numeric"; usage ;;
esac

if [ $r -ne 0 ] ; then
    resolution=$r
fi

case $resolution in
    *[^0-9]*) echo "Error: resolution must be positive numeric"; usage ;;
esac

if [ $f != 'none' ] ; then
    format=$f
fi
if [ $format != "none" ] ; then
    inarg=$format
    goodvals=( $alltos )
    prompt="Error: format arg incorrect"
    validatearg
fi


# get filename from first argument
srcfile="`basename $filename`"   

# get filename without .ly extension
STEM="`basename $filename .ly`"

# determine output directory
OUTDIR="`dirname $filename`"

if [[ $resolution -ne 0 ]] ; then
    echo Resolution set to $resolution dots per inch.
    RES=$resolution
else
    # ask for output resolution
    prompt="Enter output resolution in DPI 72, 100, 300, 600, etc...(72): "
    default=72
    getnumval
    RES=$outval
fi

# ask for desired final output format
if [ "$transparency" == 'no' ] ; then
    if [[ ( "$format" == 'gif') || ( "$format" == 'png' ) || ( "$format" == 
'none' ) ]] ; then
        # if only prompt for transparency if format unset or set to gif or png
        prompt="Transparency? y/N"
        default="N"
        goodvals=("y" "Y" "n" "N")
        getval
        TRANSPARENCY=$outval
    fi
else
    TRANSPARENCY="Y"
fi
if [[ ( "$TRANSPARENCY" == "Y" )  || ( "$TRANSPARENCY" == "y" ) ]] ; then
    transparency='yes'
    if [[ ( "$format" != 'gif') && ( "$format" != 'png' ) ]] ; then
        # if they ask for transparency and format's not gif or png
        # then ask them what they want?
        if [[ "$format" != 'none' ]] ; then
            echo "You ask for transparency which doesn't work with" $format
        fi
        prompt="Enter desired output format png, gif (png): "
        default="png"
        goodvals=("png" "gif")
        getval
        FORMAT=$outval
    else
        FORMAT=$format
    fi
else
    if [[ $format != 'none' ]] ; then
        echo Output format is $format
        FORMAT=$format
    else
        prompt="Enter desired output format jpeg, png, tiff, gif, pcx, bmp ... 
(png)  : "
        default='png'
        goodvals=( $alltos )
        getval
        FORMAT=$outval
    fi
fi

cd $OUTDIR

# run lilypond on file with png output for further processing...
lilypond --format=png -dresolution=$RES $srcfile

# The next commands crop the png file so that
# it only includes the example instead of an entire page.
# First convert image to pnm for processing with netpbm tools
pngtopnm $STEM.png > $STEM.pnm

# crop all the white space off
pnmcrop -white $STEM.pnm > $STEM-cropped.pnm

outcmd="invalid"

which >& /dev/null ppmto$FORMAT
if [ $? -eq 0 ] ; then
    outcmd=ppmto$FORMAT
else
    which >& /dev/null pnmto$FORMAT
    if [ $? -eq 0 ] ; then
        outcmd=pnmto$FORMAT
    fi
fi
if [ $outcmd == "invalid" ] ; then
    echo "Sorry, can't find a command for that format."
    exit -1
fi

# convert to end format
if [[ $transparency != 'no' ]] ; then
    $outcmd -transparent '#ffffff' $STEM-cropped.pnm > $STEM.$FORMAT
else
    $outcmd $STEM-cropped.pnm > $STEM.$FORMAT
fi

# removes pnm and ps files
rm *.pnm $STEM.ps

# open final image as background process in "Eye of Gnome" Image Viewer
eog $STEM.$FORMAT &

reply via email to

[Prev in Thread] Current Thread [Next in Thread]