#! /bin/bash# converts png to eps## 2003-04-09 Thomas Henlich## requires: pngcheck pngtopnm sed basename# pnmtotiff tiff2ps# bc (for images with non-square pixels)## Usage:# png2eps file.png > file.eps# for maximum compression set this to -lzw# (requires LZW compression in pnmtotiff and tiff2ps)COMPRESS=-packbits
#COMPRESS=-lzw# we need a tmp file for input to tiff2psOUTFILE=/tmp/png2eps.tif.$$# temp file for the tEXt chunkTXTFILE=/tmp/png2eps.txt.$$# command to remove filesRM="rm -f"# get the resolution (if specified) from the PNG fileCHK=`pngcheck -v $1`# get height in pixels from the PNG fileHEIGHT=`echo$CHK| sed -ne 's/.* [0-9]\+ x \(.*\) image.*//p;'`# a rowsperstrip parameter of 'height' (or more) is needed to generate one # single strip of data in the TIFF file (reduces file size)ROWSPERSTRIP="-rowsperstrip $HEIGHT"# ROWSPERSTRIP="rowsperstrip 1000000"# for images with square pixels, pngcheck outputs a line like# chunk pHYs ... 9646x9646 pixels/meter (245 dpi)# and we can read the resolution in dpi directlyRES=`echo$CHK| sed -ne '/pHYs/s/.*(\(.*\) dpi.*//p;'`# for images with non-square pixels, pngcheck outputs a line like# chunk pHYs ... 9646x4803 pixels/meterXRES=`echo$CHK| sed -ne '/pHYs/s/.*: \([0-9]*\)x.* pixels\/meter.*//p;'`YRES=`echo$CHK| sed -ne '/pHYs/s/.*x\([0-9]*\) pixels\/meter.*//p;'`# for images with no fixed resolution, pngcheck may output a line like# chunk pHYs ... 9646x4803 pixels/unit# we ignore it (like pdfTeX does)# set the command-line arguments for pnmtotiff which specify the resolutionif["$RES"];thenecho"png2eps: Image with square pixels ($RES dpi)"1>&2RES='-xres '$RES' -yres '$RESfiif[ -z "$RES"];thenif["$XRES"];thenecho"png2eps: Image with non-square pixels"\n"($XRES""x$YRES pixels/meter)"1>&2# we still have to convert pixels/meter -> dpiRES='-xres '`echo .0254*$XRES|bc`' -yres '`echo .0254*$YRES|bc`;else# no resolution was specified and so a default of 72 dpi will be usedecho"png2eps: No resolution specified, using default (72 dpi)"1>&2;fifi# convert to tiff
pngtopnm -verbose -text $TXTFILE$1|\n pnmtotiff $ROWSPERSTRIP$RES$COMPRESS -indexbits=1,2,4,8 >$OUTFILE# get the title of the figureTITLE="`sed -ne 's/^Title *\(.*\)//p;' <$TXTFILE`"# set to input file name if unspecified[ -z "$TITLE"]&&TITLE=`basename $1`# convert to EPS, replacing the %%Title comment
tiff2ps -2 -e -z $OUTFILE| sed -e "s/^\(%%Title: \).*/$TITLE/;"# remove temp files$RM$OUTFILE$TXTFILE
Last edit: Duy Dinh 2013-08-20
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Last edit: Duy Dinh 2013-08-20