Tuesday, May 10, 2011

png2eps: convert png image to eps with high resolution

#!/bin/bash
# convert a png file into eps file
# Usage:
#           png2eps pngfile.png [-q JPGQuality -r FigResizePercentage]
# e.g.,
#           png2eps aa.png bb.png -q 100 -r 60
# http://scriptdemo.blogspot.com

if [ $# == 0 ]; then
   sed -n '3,6p' png2eps
   exit
fi
JPGQuality='100'
FileResize='80'

nfile=0
while test -n "$1"
do
  case "$1" in
    -q|-quality)
       JPGQuality=$2
       shift
       ;;
    -r|-resize)
       FileResize=$2
       shift
       ;;
    *)
       if [ -e $1 ]; then
          pngfiles[$nfile]=$1
          nfile=`expr $nfile + 1`
       fi
       shift
       ;;
  esac
done
for pngfile in ${pngfiles[*]}
do
  #convert to jpg file first to keep high resolution
  jpgfile=${pngfile%%.*}.jpg
  epsfile=${pngfile%%.*}.eps
  eval "convert -quality ${JPGQuality} -resize ${FileResize}% ${pngfile} ${jpgfile}"
  [ -e ${epsfile} ] && epsfile=tmp_${epsfile}
  eval "convert ${jpgfile} eps3:${epsfile} && rm -f ${jpgfile}"
done
Another solution is to use png2pnm and pnm2ps commands:
#!/bin/bash
# convert a png file into eps file [using pngtopnm, pnmtops]
# Usage:
#           png2eps2 pngfile.png -scale reScale [-o outfile.eps]
# e.g.,
#           png2eps2 aa.png -scale 0.3 -o aa.eps
# http://scriptdemo.blogspot.com

if [ $# == 0 ]; then
   sed -n '3,6p' png2eps2
   exit
fi
reScale='0.3'

nfile=0
while test -n "$1"
do
  case "$1" in
    -s|-r|-scale|-rescale)
       reScale=$2
       shift
       ;;
    -o|-output)
       epsfile=$2
       shift
       ;;
    *)
       if [ -e $1 ]; then
          pngfiles[$nfile]=$1
          nfile=`expr $nfile + 1`
       fi
       shift
       ;;
  esac
done
for pngfile in ${pngfiles[*]}
do
  #convert to jpg file first and resize
  [ ! -n "$epsfile" ] && epsfile=${pngfile%%.*}.eps
  [ $nfile -gt 1 ] && epsfile=${pngfile%%.*}.eps
  [ -e ${epsfile} ] && epsfile=tmp_${epsfile}
  eval "pngtopnm ${pngfile} | pnmtops -noturn -nocenter -scale ${reScale} - >${epsfile}"
  eps2eps -dLanguageLevel=3 ${epsfile} thisTmp${epsfile} && mv thisTmp${epsfile} ${epsfile}
done

3 comments:

Anonymous said...

Thanks. I tested the first script and it works. I think it might be a good idea to list dependencies (such as convert). I have a lot of libraries and packages on my system so I didn't run into any trouble but someone else might :).

who said...

you're right. thx for you comment!
I coded a script to check the existence of a cmd this month.

But didn't get time to update those old scripts yet. Also I am wondering to provide the original script files in the future if i get enough time and proper way to to that.

Anyway, people can do whatever they like to use those scripts from this website. I'm kind of lazy sometime. Maybe it's good for people to understand those scripts better. :)

who said...

The link to check the existence of a command. Just a simply way to check whether you can find it in you PATH.

http://scriptdemo.blogspot.ca/2012/05/bash-check-existance-of-command.html

ShowCalendar