作者 Anonymous [actionscript] 2010-11-11 17:58 (点击下载)

  1. #!/usr/bin/perl
  2. #
  3. # ps2png.pl Norbert Haider, University of Vienna, 2005
  4. #
  5. # Script reads a Postscript file (obtained from a MDL molfile
  6. # with the utility program mol2ps) and generates a 2D
  7. # graphical images in PNG format by piping the PS file through
  8. # Ghostscript.
  9. #
  10. # Ghostscript must be installed and the "gs" command must be in
  11. # your search path.
  12.  
  13.  
  14. # =============== user customizable parameters: ===============
  15.  
  16. $scalingfactor = 0.22; # 0.22 gives good results
  17.  
  18. # =============================================================
  19.  
  20. if ($#ARGV < 0) {
  21. print "Usage: ps2png.pl <inputfile>\n";
  22. exit;
  23. }
  24.  
  25. $infile = $ARGV[0];
  26.  
  27. $outfile = $infile;
  28. if (index($outfile,'.') > -1) {
  29. $outfile = substr($outfile,0,rindex($outfile,'.'));
  30. }
  31. $outfile = $outfile . '.png';
  32.  
  33. $molps = "";
  34.  
  35. open (PSFILE, "<$infile") || die ("cannot open input file $infile!");
  36.  
  37. while ($line = <PSFILE>) {
  38. $line =~ s/\r//g; # remove carriage return characters (DOS/Win)
  39. $molps = $molps . $line;
  40. }
  41.  
  42.  
  43. $bb = filterthroughcmd($molps,"gs -q -sDEVICE=bbox -dNOPAUSE -dBATCH -r300 -g500000x500000 - ");
  44. @bbrec = split(/\n/, $bb);
  45. $bblores = $bbrec[0];
  46. $bblores =~ s/%%BoundingBox://g;
  47. chomp($bblores);
  48. $bblores = ltrim($bblores);
  49. @bbcorner = split(/\ /, $bblores);
  50. $bbleft = $bbcorner[0];
  51. $bbbottom = $bbcorner[1];
  52. $bbright = $bbcorner[2];
  53. $bbtop = $bbcorner[3];
  54. $sf = $scalingfactor;
  55. $xtotal = ($bbright + $bbleft) * $sf;
  56. $ytotal = ($bbtop + $bbbottom) * $sf;
  57. if (($xtotal > 0) && ($ytotal > 0)) {
  58. $molps = $sf . " " . $sf . " scale\n" . $molps; ## insert the PS "scale" command
  59. #print "low res: $bblores .... max X: $bbright, max Y: $bbtop \n";
  60. print "writing file $outfile with output dimensions of $xtotal x $ytotal pt\n";
  61. } else {
  62. $xtotal = 99;
  63. $ytotal = 55;
  64. $molps = "%!PS-Adobe
  65. /Helvetica findfont 14 scalefont setfont
  66. 10 30 moveto
  67. (2D structure) show
  68. 10 15 moveto
  69. (not available) show
  70. showpage\n";
  71. print "writing empty file\n";
  72. }
  73. $gsopt1 = " -r300 -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -dDEVICEWIDTHPOINTS=";
  74. $gsopt1 = $gsopt1 . $xtotal . " -dDEVICEHEIGHTPOINTS=" . $ytotal;
  75. $gsopt1 = $gsopt1 . " -sOutputFile=" . $outfile;
  76. $gscmd = "gs -q -sDEVICE=pnggray -dNOPAUSE -dBATCH " . $gsopt1 . " - ";
  77. system("echo \"$molps\" \| $gscmd");
  78.  
  79.  
  80. # =============================================================
  81.  
  82. sub filterthroughcmd {
  83. $input = shift;
  84. $cmd = shift;
  85. open(FHSUB, "echo \"$input\"|$cmd 2>&1 |"); # stderr must be redirected to stdout
  86. $res = ""; # because the Ghostscript "bbox" device
  87. while($line = <FHSUB>) { # writes to stderr
  88. $res = $res . $line;
  89. }
  90. return $res;
  91. }
  92.  
  93. sub ltrim() {
  94. $subline1 = shift;
  95. $subline1 =~ s/^\ +//g;
  96. return $subline1;
  97. }

提交下面的校正或者修改. (点击这里开始一个新的帖子)
姓名: 在 cookie 中记住我的名字

屏幕抓图:(jpeg 或 png)