Question

I've got a problem converting a pdflatex-generated PDF image to a PNG image using the standalone package.

The pixelated rendering of the text in the converted image (PDF->PS->PNG via gs and ImageMagick?) is awfully blurry and inferior in quality (sharpness, crispness etc.) to the screen-dumped original PDF.

I have checked out these StackExchange posts:

and been guided in the setup of my workflow by the standalone package manual. But after considerable experimental adjustment of the various conversion settings in the code below, I have been unable to improve the quality of the outputted PNG image.

A sample of the settings I have played with:

  • density (increase the dpi)
  • size (increase/decrease the dimensions)
  • the TikZ picture width/height dimensions (no optimum found, but if too small the PNG image width does not equal that specified among the documentclass parameters)
  • using the command={} option, I have also played with options such as -quality and - set colorspace RGB (though I didn't really know what I was doing here)

Another approach I have taken is to try to set the TikZ picture width and height dimensions (in cm) in such a way that they agree with the conversion dimensions given among the documentclass parameters (using a dpi + pixels -> cm converter).

None of this worked! So any help in converting from PDF to PNG using the standalone package that preserves the sharpness and crispness of the rendered text in the image would be hugely appreciated.

For reference the versions of the various systems/applications I'm using are:

  • Windows 7
  • MiKTeX 2.9
  • TeXnicCenter
  • gs 9.09
  • ImageMagick 6.8.6 Q16 (32-bit)
  • standalone package installed using MiKTeX package manager late Aug 2013

\documentclass[preview,convert={density=300,size=900x300,outext=.png}]{standalone}

\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgfplots}

\begin{document}

\pgfplotsset{every x tick label/.style={at={(1,0)}, yshift=-0.15cm, xshift=-0.0cm,         inner sep=0pt, font=\normalsize}}

\begin{tikzpicture}
\begin{axis}[
no markers, domain=-2.1*pi:2.1*pi, samples=1000,
width=30.0cm,
height=10.0cm,
axis x line*=middle,
x axis line style={densely dotted, opacity=0.75},
axis y line*=middle,
y axis line style={densely dotted, opacity=0.75},
ymin=-1.1,
ymax=1.1,
xtick={-6.28318530717959, -5.65486677646163, -5.02654824574367, -4.71238898038469,     -4.39822971502571, -3.76991118430775, -3.14159265358979, -2.51327412287183, -1.88495559215388, -1.5707963267949, -1.25663706143592, -0.628318530717959, 0, 0.628318530717959, 1.25663706143592, 1.5707963267949, 1.88495559215388, 2.51327412287183, 3.14159265358979, 3.76991118430775, 4.39822971502571, 4.71238898038469, 5.02654824574367,     5.65486677646163, 6.28318530717959},
xticklabels={$-2\pi$, $-\frac{9\pi}{5}$, $-\frac{8\pi}{5}$, $-\frac{3\pi}{2}$, $-\frac{7\pi}{5}$, $-\frac{6\pi}{5}$, $-\pi$, $-\frac{4\pi}{5}$, $-\frac{3\pi}{5}$, $-\frac{\pi}{2}$, $-\frac{2\pi}{5}$, $-\frac{\pi}{5}$, $0$, $\frac{\pi}{5}$, $\frac{2\pi}{5}$, $\frac{\pi}{2}$, $\frac{3\pi}{5}$, $\frac{4\pi}{5}$, $\pi$, $\frac{6\pi}{5}$,     $\frac{7\pi}{5}$, $\frac{\pi}{2}$, $\frac{8\pi}{5}$, $\frac{9\pi}{5}$, $2\pi$},
ytick=\empty,
enlargelimits=false, clip=true, axis on top]
\addplot [line width=0.5,cyan!50!black] {sin(deg(5*x))*cos(deg(x)};
\end{axis}
\end{tikzpicture}
\end{document}
Was it helpful?

Solution

In order to investigate this problem I first created a PDF from your posted tikz/tex code (after copying it into a tikz.tex file):

pdflatex tikz.tex
pdflatex tikz.tex

The resulting PDF does contain the illustration as a vector graphic, not a raster image. Hence, pdfimages -list will NOT detect it.

Then I tested two ways to convert the resulting PDF file to a PNG:

  1. Using ImageMagick's convert (which employs Ghostscript behind your back as a 'delegate' to process the PDF input)
  2. Using Ghostscript directly

1. Using convertwith -density 720

I've used this command to create a PNG from the PDF:

convert -density 720 tikz.pdf tikz1.png

Here is the result:

tikz1.png

Why did I use -density 720? Because 720 PPI is the default resolution which Ghostscript uses when creating PDFs (unless you override this default setting by providing your own via -rNxM on the gs command line)...

The resulting image has a size of 374 kB (the PDF had 49 kB) and a width x height dimension of 8060 x 2390 pixels. Any pixelization (which will happen whenever you create a PNG!) is not immediately visible at that resolution.

The runtime for a loop running this command 10 times was 47 seconds.

2. Using Ghostscript directly

To achieve the direct PNG conversion with a Ghostscript command I used:

gs -o tikz-gs.png    -sDEVICE=pngalpha     \
   -dAlignToPixels=0 -dGridFitTT=2         \
   -dTextAlphaBits=4 -dGraphicsAlphaBits=4 \
    tikz.pdf

Here is the resulting PNG:

tikz-gs

It's file size is 308 kB, with dimensions of 8060 x 2390 pixels.

The runtime for a loop running this command 10 times was 17 seconds.

Analysis

PDF input file: Sizes of components

After looking at the source code of the PDF file after uncompressing all objects, I came up with the following statistics:

Total size of 5 embedded Type1 fonts................................ 38615 Bytes
Total size of v`/Contents` stream (mainly used by vector drawing)... 32630 Bytes
Rest of PDF structure ("overhead", if you want).....................  5827 Bytes
---------------------------------------------------------------------------------
Total size of PDF (after uncompressing objects)..................... 77072 Bytes

The fonts are Type 1 (i.e. PostScript) fonts, according to the output of pdffonts. They are all embedded as subsets:

pdffonts tikz.pdf
 name                       type         encoding         emb sub uni object ID
 -------------------------- ------------ ---------------- --- --- --- ---------
 FXXUVH+CMSY10              Type 1       Builtin          yes yes no       7  0
 BCSIZL+CMR10               Type 1       Builtin          yes yes no       8  0
 SFJZUV+CMMI10              Type 1       Builtin          yes yes no       9  0
 WPSSUY+CMR7                Type 1       Builtin          yes yes no      10  0
 SYHYOI+CMMI7               Type 1       Builtin          yes yes no      11  0

Because...

  • ...Fonts (unless they are raster fonts) are a different way to very efficiently code vector shapes for glyphs depicting text characters,

  • ...Fonts + Vector drawing compose more than 90% of the total PDF size,

...there is no way in hell that lets you create a PNG raster image from the (compressed) PDF sized 49 kB (uncompressed size was 75 kB) that isn't larger by a few times than the original PDF file if you want to avoid directly visible "pixelization" and "blur".

Even if you use a resolution of 720 PPI (which creates a 308 kB-sized PNG), you'll still see pixelization once you start zooming in. Such pixelization does not occur with the PDF (because all its shapes are defined as vectors).

The following three images are screenshots:

  • top, from the tikz.pdf file at a high zoom level (~1000%),
  • center, from the tikz.png created with 720 PPI (at a similar zoom level),
  • bottom, from the tikz72.png created with 72 PPI (at a similar zoom level):

tikz.pdf

tikz.png

tikz72.png

The text sizes used for the annotation of the coordinate axis are only around 10 points. If you rasterize those, you'll get clearly visible pixelization at any resolution below 400 PPI, maybe even above...


My Ghostscript is a self-compiled 9.17 GIT PRERELEASE. My ImageMagick is 6.9.0-0 Q16 x86_64.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top