# How to run external program in Mathematica?

In fact, I want to do the following in some kind of sense automatically, thus a windows bash file or Mathematica code will be welcome.

Suppose I have a pdf file, lets name it as test.pdf (you can download it here), which is with low-resolved, i.e., not clear for reading. I have do a lot of efforts to improve it.

My first try will use Mathematica to process it, such as Sharpen, ImageAdjust, and so on. It does some part of work, but I am not quite satisfied. If you have any suggestion in this direction, please visit this question for more detail.

Then, I have been suggested that I should use some external program, such as Inkscape or Potrace, and after a manual test, I find that potrace works well.

Here is my work flow:

• Since Potrace only work for bmp files as input, I need to extract all the pages of test.pdf to bmp files. I think this can be done by Mathematica, but I did not know the exact script for which this will work. Anyway, I try to use Import and Export to do this but it seems that this pdf file is broken... so I just extract all the pages to PNG files through Adobe Acrobat, and convert them to BMP files by applying the following code:

FileList = FileNames["*.png"];
Do[Export["new\\" <> FileBaseName[i] <> ".bmp", Import[FindFile[i]]], {i, FileList}]

• Now we have BMP image files, then we can use Potrace to do the following work, in win bash, run:

mkbitmap -s 2 -t 0.5 *.bmp

which will scale all the image to double size and use threshold 0.5 (which can be find by Mathematica) to pre-process the images (see the manual page for detail).

Next, convert the bitmap to vector graphics, this can be done as:

potrace -b pdf *.pbm

• And at last, we can merge all the resulting pdf files into one by Adobe Acrobat.

My question is how to make all the above more automatically, either use Mathematica to run the external programs mkbitmap and potrace or write a win bash to load Mathematica (we know that we can run it through math or MathKernel)?

-
Of course, any enhancement will be appreciated. –  van abel Jun 5 at 15:53
I would also like to share the resulting pdffiles:lttt.blog.ustc.edu.cn/wp-content/uploads/2013/06/test_new.pdf –  van abel Jun 5 at 15:59
OT: are you a student in ustc?? I studied there years ago :D –  Silvia Jun 5 at 16:14
You will be surprised by what you'll see if you enter the first five words of the title of your post in the search bar of Mathematica's documentation center (at least, if you have an English version). –  Sjoerd C. de Vries Jun 5 at 18:08
For some scripting of external programs you may check my answere here. –  Sjoerd C. de Vries Jun 6 at 5:20