|
From: | Patrick Horgan |
Subject: | Re: png cropping |
Date: | Thu, 18 Sep 2008 15:38:05 -0700 |
User-agent: | Thunderbird 2.0.0.16 (X11/20080724) |
Jonathan Kulp wrote:
Ok I changed a couple of things to make it even more flexible. I changed pnmto__ to ppmto__, giving quite a few more output options. I've suggested a few in the script. Also added an option to choose either gif or png when choosing transparent background. this is fun :)Jonathan, on my system there's no ppmtopng or ppmtotiff and ppmtojpeg is a link to pnmtojpeg. This is with Hardy Ubuntu and a fresh install of the utils. Annoying, no? It makes the changes to your script break on my machine. I've found that creating symbolic links in /usr/bin like: ln -s pnmtopng ppmtopng ln -s pnmtotiff ppmtotifffixes the problem, but why wouldn't that have been done automagically? I removed the netpbm package and it didn't remove my links, so they're really not part of the package. Maybe I should file a bug against the package. It does reinstall the link from ppmtojpeg to pnmtojpeg when I reinstall it. How is it set up on your system? It seems like to be bulletproof, the script needs to check and see which of ppmto$FORMAT and pnmto$FORMAT exists and use that, that's easy to do with which, it already has all the code to look through your PATH. I'm attaching a new version of my version of your script with that change and a new routine, getval, that validates input, you use it like: prompt= "Enter desired output format (jpeg, png, tiff, gif, pcx, bmp): " goodvals=("jpeg" "png" "tiff" "gif" "pcx" "bmp") getval FORMAT=$outval If the user enters something that's not on the goodvals list, for example joe, it reprompts them after telling them what they might have entered: Enter desired output format (jpeg, png, tiff, gif, pcx, bmp): joe Expecting one of : jpeg png tiff gif pcx bmp Enter desired output format (jpeg, png, tiff, gif, pcx, bmp):Cool, no? I suppose I should add validation to the command line arguments as well. Ok, just did that, resolution checked for numeric both on input and from the command line, format checked for one of the allowable things, and check to make sure there's a filename, and print meaningful error message and a usage statement. Try it and see if it works for you. I'm almost tempted to check for all the pnmto and ppmto and automatically build the list of allowed types instead of hard coding them--nah, I'll leave that as an exercise for the reader;) It really needs to be done with my version because I only let people use what's on the list:( Yours will just work as long as it's a valid format. You could grab the outcmd part of my version of your script. It automagically picks either ppmto or pnmto and exits with an error if neither are found. I also check for the existence of pnmtojpeg and abort the script if not found with a message that the netpbm utilities have to be installed to use the script. Now the script is much larger because of all the checking. 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. # #*****************************************************# #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # usage is called when we're called incorrectly. # it never returns #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ usage() { echo "Usage: " $0 " [-t] [-rN] [-fFORMAT] filename" 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 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 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 case $inval in *[^0-9]*) echo "Error: expecting positive numeric value" ;; * ) flag="done" ;; esac 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 while [ "$index" -lt "$elementcount" ] ; do if [ ${goodvals[$index]} == $inval ] ; then flag="good" outval=${goodvals[$index]} fi let index++ done 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 } which >& /dev/null pnmtojpeg if [ $? -ne 0 ] ; then echo "Sorry, you have to have the netpbm utilities installed to use this." exit 0 fi transparency='no' t='no' resolution=0 r=0 format='none' f='none' filename="none" getopt_simple $* 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=("jpeg" "png" "tiff" "gif" "pcx" "bmp") 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.): " getnumval RES=$outval fi # ask for desired final output format if [[ $transparency != 'no' ]] ; then echo Output format forced to png for transparency FORMAT='png' else prompt="Transparency? y/N" goodvals=("y" "Y" "n" "N") getval TRANSPARENCY=$outval if [[ ( "$TRANSPARENCY" == "Y" ) || ( "$TRANSPARENCY" == "y" ) ]] ; then transparency='yes' prompt="Enter desired output format (png, gif): " goodvals=("png" "gif") getval FORMAT=$outval else if [[ $format != 'none' ]] ; then echo Output format is $format FORMAT=$format else prompt="Enter desired output format (jpeg, png, tiff, gif, pcx, bmp): " goodvals=("jpeg" "png" "tiff" "gif" "pcx" "bmp") getval FORMAT=$outval fi 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 &
[Prev in Thread] | Current Thread | [Next in Thread] |