[Thread Prev][Thread Next][Index]

[ferret_users] Re: producing eps and png from postscript



Dear Ferret users,


a small update on this thread, with my latest solution (been working
reliably for years now).  

to generate eps, I generate a ps file with Fprint, then I use 

   cat ferret_file.ps | ps2eps -B  > plot.eps

This converts and crops reliably and generates valid eps files.  The
tool is available in the ps2eps package (Ubuntu/Debian).
Alternatively these days I go straight to png via a much faster (all
in memory) process:

gs -dSAFER -dNOPAUSE -r300  -dDEVICEWIDTHPOINTS=864  \
   -dDEVICEHEIGHTPOINTS=864 -sDEVICE=ppmraw -q -sOutputFile=- \
   -f ferret_file.ps -c showpage -c quit \
   | pnmcrop | convert pnm:- PNG:${tmppng}

where ferret_file.ps is the output of Fprint, and ${tmppng} is a
temporary png output file. I write the output under /dev/shm to keep
the file in memory.  I then antialias and scale this (huge) pngfile with

convert -antialias -scale 35% -rotate 270 -type Palette \
	${tmppng} PNG8:output.png

The above requires ghostscript to be installed (gs) as well as pnmcrop
which is part of the netpbm package.  Finally it uses convert from
imagemagick.  

The step with /dev/shm can be avoided by piping the temporary png
file straight to the last convert command, but I want to create 2
sizes of png file, so I need the intermediate file. The gs device width
and height can be increased further if cropping problems with the page
margins occur.


Kind regards,
     Hein


-- 

Dr. Hein Zelle
Senior consultant meteorology & oceanography
BMT ARGOSS

Tel:        +31 (0)527-242299
Fax:        +31 (0)527-242016
E-mail:     hein.zelle@xxxxxxxxxxxxx
Website:    www.bmtargoss.com
	    
BMT ARGOSS b.v.
Voorsterweg 28, 8316 PT Marknesse, the Netherlands
Postal address: P.O. Box 61, 8325 ZH Vollenhove, the Netherlands

Registered in The Netherlands, Registered no. 39060160.

Unless otherwise agreed by BMT ARGOSS in writing, all work,
services, goods or products supplied by BMT ARGOSS shall be subject
to and governed by BMT ARGOSS' own terms and conditions which are
available for inspection from BMT ARGOSS on request.

E-mail confidentiality notice and disclaimer:
The contents of this e-mail and any attachments are intended for the
use of the mail addressee(s) shown. If you are not that person, you
are not allowed to read it, to take any action based upon it or to
copy it, forward, distribute or disclose the contents of it and you
should please delete it from your system. BMT ARGOSS does not accept
liability for any errors or omissions in the context of this e-mail or
its attachments which arise as a result of internet transmission, nor
accept liability for statements which are those of the author and
clearly not made on behalf of BMT ARGOSS.
#!/bin/bash

inputfn="metafile.plt"
outputfn="output.ps"
myname=`basename $0`
convert="ps2eps -B"
# makepng="convert +antialias -density 300 -rotate 270"
# makethumb="convert +antialias -density 37 -rotate 270"
makesmall="convert -antialias -scale 35% -rotate 270 -type Palette"
makethumb="convert -antialias -scale 12% -rotate 270 -type Palette"
background=0
thumbnail=0
rotate=270

# where to copy the metafile to 
tmpname=/dev/shm/fprint_metafile_$$.tmp
tmpps=/dev/shm/ferret-tmp-ps-$$.ps
tmppng=/dev/shm/ferret-tmp-png-$$.png

# make a subroutine to process a file.
# this makes it possible to continue in the background and exit the script.
# the input filename is passed in $1
process_file()
{
    if [ $myname = "bwprint" -o $myname = "rbwprint" ] ; then
	# echo -n "Fprint -l ps -R -o $outputfn $1 ... "
	# Fprint -l ps -R -o $tmpps $1
	echo -n gksm2ps -l ps -R -d cps -a -o $tmpps $1 ...
	gksm2ps -l ps -R -d cps -a -o $tmpps $1
    else
	# echo -n "Fprint -l cps -R -o $outputfn $1 ... "
	# Fprint -l cps -R -o $tmpps $1
	echo -n gksm2ps -l cps -R -d cps -a -o $tmpps $1 ...
	gksm2ps -l cps -R -d cps -a -o $tmpps $1
    fi
    echo "done."
    rm -v $1
    
    # get a basename for the file wether it's called .eps or .png
    basename=$(basename $(basename $outputfn .eps) .png)
    pngbase=${basename}.png
    pngfile=`dirname $outputfn`/${pngbase}

    # Hein Zelle, 27-12-2010
    # add a fixed papersize of 12"x12".  This is roughly the long side
    # of an A4 sheet, squared.  (1 point = 1/72 inch)
    # this prevents margin problems and legends getting clipped during
    # the postscript rendering, hopefully.

    echo "trimming postscript, converting to PNG"
    echo "gs -dSAFER -dNOPAUSE -r300 -dDEVICEWIDTHPOINTS=864 -dDEVICEHEIGHTPOINTS=864 -sDEVICE=ppmraw -q -sOutputFile=- -f $tmpps -c showpage -c quit | pnmcrop | convert pnm:- PNG:${tmppng}"
    gs -dSAFER -dNOPAUSE -r300  -dDEVICEWIDTHPOINTS=864 -dDEVICEHEIGHTPOINTS=864 -sDEVICE=ppmraw -q -sOutputFile=- -f $tmpps -c showpage -c quit | pnmcrop | convert pnm:- PNG:${tmppng}

    echo "$makesmall $tmppng PNG8:${pngfile}"
    $makesmall $tmppng PNG8:${pngfile}
    
    if [ "$thumbnail" = "1" ] ; then
	thumbdir=`dirname $outputfn`/thumbnails
	thumbfile=${thumbdir}/${pngbase}
	
        # make thumbnails directory if needed
	if [ ! -d $thumbdir ] ; then
	    mkdir $thumbdir
	fi
	
        # make the thumbnail
	echo "$makethumb $pngfile PNG8:${thumbfile} ... "
	$makethumb $tmppng PNG8:${thumbfile}
    fi

    # clean up
    echo "cleaning up temporary files under /dev/shm"
    rm -v $tmpps $tmppng
    
    echo "processing of $outputfn done."
}

# main script starts here

if [ "$1" = "-h" -o "$1" = "--help" ] ; then
	echo ""
	echo "Usage: $myname [-t] [-b] [-r angle] [input.plt] <output.png>"
        echo "    Input is optional, 'metafile.plt' assumed by default."
        echo "    If no arguments are given, values are requested interactively."
	echo "    -t generates a thumbnail in thumbnails/ directory"
	echo "    -b runs postscript processing in the background"
	echo "    -r rotates the final picture by angle degrees (default 270)"
	echo ""
        echo "DO NOT CHANGE THE ORDER OF -t, -b, -r !"
	echo ""
	echo "Converts a Ferret metafile to PNG with a proper boundingbox."

	echo $myname | egrep -q "^r"
	if [ "$?" = "0" ] ; then 
		echo "Input metafile is removed after processing."
	fi

	echo ""
	exit 0
fi

# check for --thumbnail or -t option
# remove the $1 argument (shift) and continue normally
if [ "$1" = "--thumbnail" -o "$1" = "-t" ] ; then
    thumbnail="1"
    shift 1
fi

if [ "$1" = "--background" -o "$1" = "-b" ] ; then
    background="1"
    shift 1
fi

if [ "$1" = "--rotate" -o "$1" = "-r" ] ; then
    rotate="$2"
    shift 2
fi

if [ "$1" = "" ] ; then
	# no arguments, ask interactively
	echo -n "enter input filename [metafile.plt] ? "
	read tmpin
	if [ $tmpin != "" ] ; then 
	    inputfn=$tmpin
	fi

	echo -n "enter output filename [output.eps] ? "
	read tmpout
	if [ $tmpout != "" ] ; then
	    outputfn=$tmpout
	fi
else
	if [ "$2" = "" ] ; then
		# one argument
		outputfn=$1
	else
		# two arguments (or more)
		inputfn=$1
		outputfn=$2
	fi
fi

# run in the background if requested
# check if inputfn exists before continuing.

if [ ! -f $inputfn ] ; then
    echo "$0: ${inputfn}: no such metafile found."
    exit 1
fi

if [ "$background" = "1" ] ; then
    echo "processing $inputfn in the background."
    cp $inputfn $tmpname
    (process_file $tmpname) &
else
    echo "processing $inputfn in the foreground."
    cp $inputfn $tmpname
    (process_file $tmpname)
fi

# remove the original metafile if requested (program name starts with r)
echo $myname | egrep -q "^r"
if [ "$?" = "0" ] ; then 
    rm -v $inputfn
fi

# done - script exits here


[Thread Prev][Thread Next][Index]
Contact Us
Dept of Commerce / NOAA / OAR / PMEL / Ferret

Privacy Policy | Disclaimer | Accessibility Statement