Showing posts with label convert. Show all posts
Showing posts with label convert. Show all posts

Wednesday, June 6, 2012

[Bash] ps2png: convert ps to png format with high quality

previous solution using ImageMagicK works too, but may not produce the high quality expected.

#!/bin/bash
# convert a single-page ps file into png format using gs
# usage:
#            ps2png psfile.ps
# Note:
#           1: need gs installed
#           2: same name is used for the png file
#           http://scriptdemo.blogspot.com

if [ $# == 0 ]; then
   sed -n '2,4p' `which ps2png`
   exit
else
  for psname in $*
  do
     if [ -e $psname ]; then
        pngname=`echo ${psname%%.*}`
          gs -r300 -dTextAlphaBits=4 -sDEVICE=png16m -sOutputFile=${pngname}.png -dBATCH -dNOPAUSE ${psname}
     else
        echo "${psname} does not exist!"
     fi
  done
fi

Friday, May 18, 2012

[Bash] ris2bib: convert RIS citation file to bibtex format

#!/bin/bash
# convert ris format to bibtex format
# usage:
#          ris2bib.sh ris-file(s)
#          www.scriptdemp.blogspot.ca

getCTag()
{
   for cTag in $*
   do
      nl=`grep "^${cTag}" ${ris} | wc -l`
      [ ${nl} -ne 0 ] && echo "${cTag}" && exit
   done
   echo ""
}

[ $# -eq 0 ] && sed -n '2,5p' `which ris2bib.sh` && exit

for ris in $*
do
    if [ -e ${ris} ]; then
       dos2unix ${ris}
       auTag=`getCTag AU A1 A2`
       tlTag=`getCTag T1 TI`
       joTag=`getCTag JO JA`
       yrTag=`getCTag PY Y1`
       doTag=`getCTag M3 DO`

       voTag="VL"; spTag="SP"; epTag="EP"; urTag="UR"; isTag="IS"
       publisher=`grep "^PB\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/^\ //g'`

       authorList0=`grep "^${auTag}\ " ${ris} | awk -F\- '{print $2"@"}'`
       authorList=`echo ${authorList0} | sed -e 's/^\ //g' -e 's/\.\ /\./g' -e "s/\@$//" | sed -e 's/@/\ and\ /g'`

       titleStr=`grep "^${tlTag}\ " ${ris} | cut -c7-`
       jourStr=`grep "^${joTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/^\ //g'`
       volStr=`grep "^${voTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/\ //g'`
       SPStr=`grep "^${spTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/\ //g'`
       EPStr=`grep "^${epTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/\ //g'`
       if [ ${#EPStr} -ne 0 ]; then
          pageStr="${SPStr}--${EPStr}"
       else
          pageStr=${SPStr}
       fi
       if [ ${#isTag} -ne 0 ]; then
          isnStr=`grep "^${isTag}\ " ${ris} | cut -c7-`
       fi
       yearStr=`grep "^${yrTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/\ //g' | cut -c-4`
       if [ ${#doTag} -ne 0 ]; then
          doiStr=`grep "^${doTag}\ " ${ris} | awk -F\- '{print $2}' | rev | awk -F\: '{print $1}' | rev | sed -e 's/^\ //g'`
       fi

       if [ ${#urTag} -ne 0 ]; then
          urlStr=`grep "^${urTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/^\ //g'`
       fi

       #generate the key
       mykey=`echo ${authorList} | awk -F\, '{print $1}'`
       mykey="${mykey}${yearStr}"
       titleKey=`echo ${titleStr} | awk '{print $1""$2}'`
       mykey=`echo "${mykey}${titleKey}" | awk '{print tolower($0)}' | sed -e 's/\-//g'`
       echo ""
       echo @article{"${mykey},"
       echo " author={${authorList}},"
       echo " title = {{${titleStr}}},"
       echo " journal = {${jourStr}},"
       echo " year = {${yearStr}},"
       echo " volume = {${volStr}},"
       [ ${#isnStr} -ne 0 ] && echo " number = {"${isnStr}"},"
       [ ${#doiStr} -ne 0 ] && echo " doi = {"${doiStr}"},"
       [ ${#urlStr} -ne 0 ] && echo " url = {${urlStr}},"
       if [ ${#publisher} -eq 0 ]; then
          echo " pages = {${pageStr}}"
       else
          echo " pages = {${pageStr}},"
          echo " publisher = {${publisher}}"
       fi
       echo " }"
       unset auTag tlTag joTag voTag spTag epTag isTag
   else
      echo ""
      echo "${ris} is not found!"
      echo ""
   fi
done

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

Wednesday, January 20, 2010

[Bash] ps2png

New version is here ps2png
#!/bin/bash
# To convert a ps file into png format
# Usage:
#            ps2png [psfile.ps]
# @ http://scriptdemo.blogspot.com

if [ $# == 0 ]; then
   for psname in *.ps
   do
         if [ -e $psname ]; then
            pngname=`echo ${psname%%.*}`
            convert $psname ${pngname}.png
         else
            echo "${psname} does not exist!"
         fi
   done
else
   for psname in $*
   do
        if [ -e $psname ]; then
           pngname=`echo ${psname%%.*}`
           convert $psname ${pngname}.png
        else
           echo "${psname} does not exist!"
        fi
   done
fi

ShowCalendar