Monday, November 24, 2008

How to convert video into a sequence of images.

under linux:
mplayer
-vo jpeg quality=100 -fps 30 mymovie.avi

Wednesday, November 12, 2008

Creating Good Quality EPS Graphics Using OpenOffice

If you use dvips and then ps2pdf to generate your PDF documents, you're no doubt using encapsulated postscript (EPS) files for your figures. The advantage to using EPS over formats like JPEG, GIF, or PNG is that EPS is a Vector Graphics format, which means that you can scale the image up or down without getting pixelation (check out the example on the Wikipedia page). For that reason, EPS graphics are very convenient to use in your latex documents because you can resize your figures easily depending on the document type. For example, if your document is in double column format, your figures will typically be 3.5 inches wide. If you change to single column format, you may want to enlarge your figures to 4.5-6.0 inches. If your images were GIF files, you may get pixelation, but EPS files will scale easily.

Furthermore, if you are using JPG/GIF files, for best results, you typically should create them knowing what size you'll be using (eg. 3.5in x 2in), and then import them without scaling them into your document. If you decide later on to scale them, you may run into pixelation problems again.

In short, the reason EPS graphics are preferred for Latex documents is because they offer greater flexibility with regards to scaling (no pixelation). But, there's a catch, you need to generate your EPS files properly, otherwise, you may end up with fuzzy blurred figures anyway.

Fuzzy/Blurred Figures

If you use EPS, you've probably noticed that your figures sometimes look fuzzy when viewed in your final PDF. You may have one or both of the following situations:

  • Lines and shapes appear fuzzy.
  • Text (fonts) appear fuzzy.

In most cases, the PDF will print properly, but the electronic version looks fuzzy in PDF because of resizing (viewing at 120% or 'page width', etc). The reason for this is that your EPS file was not properly generated by whatever program you used. Quite often, when using the 'save as' or 'export' feature of a program, the resulting EPS file doesn't have fonts embedded. You can check this by doing 'ps2pdf' and then 'pdffonts' to see if your fonts were embedded.

Fonts not embedded

If you have a figure with text where the fonts have not been embedded, you'll see no fonts listed by pdffonts:

$ ps2pdf14 bad.eps
$ pdffonts bad.pdf
name type emb sub uni object ID
------------------------------------ ------------ --- --- --- ---------

Fonts properly embedded

If your fonts have been properly embedded, you'll see something like the following output when running pdffonts:

$ ps2pdf14 good.eps
$ pdffonts good.pdf
name type emb sub uni object ID
------------------------------------ ------------ --- --- --- ---------
BAAAAA+ArialMT TrueType yes yes yes 9 0

Generating 'Good' EPS Images

Many programs support EPS as a format you can save/export to, but they don't embed fonts properly. I've found two solutions to this problem, the first is to 'print-to-file' using a postscript printer driver, the second is to export to PDF using OpenOffice

Print to file

One of the easiest ways is to install a postscript printer driver and use the 'print to file' option. When you print to file, you'll end up with a postscript file which is a letter sized page. Since your figure is most likely smaller than a full page, you'll need to use something like 'ps2epsi' or 'ps2eps' to fix the bounding box. You should see after running 'pdffonts' on the resulting PDF file that all the fonts have been properly embedded.

Export to PDF (OpenOffice)

The best way I've found to create good EPS files is:

  • Use OpenOffice Draw to draw my figures.
  • Use the "Export to PDF" function because it embeds fonts properly.
  • Use 'pdftops -eps' to generate EPS files (note: not 'pdf2ps').
  • Optional: use ps2eps to fix the bounding box of the generated EPS file is needed.
  • Here's an example:

    $ pdftops -eps test.pdf
    $ cat test.eps | ps2eps > test2.eps
    Input files: -
    Processing: -
    Rendering with existing %%BoundingBox: 0 0 792 612
    Calculating Bounding Box...ready. %%BoundingBox: 32 47 760 569
    Creating output file -...ready.

    $ ps2pdf test.eps
    $ pdffonts test.pdf
    name type emb sub uni object ID
    ------------------------------------ ------------ --- --- --- ---------
    BAAAAA+ArialMT TrueType yes yes yes 9 0

    $ pdffonts test2.pdf
    name type emb sub uni object ID
    ------------------------------------ ------------ --- --- --- ---------
    MFZMRR+ArialMT TrueType yes yes no 8 0

    As you can see, the 'test.pdf' and 'test2.pdf' contain embedded fonts. So if you were to use the 'test.eps' file in your latex document, the resulting figure will not have fuzzy fonts when resizing the image.

    NOTE: If you simply convert a GIF to EPS, you're basically getting a GIF file embedded into the postscript file.