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:
- Using ImageMagick's
convert
(which employs Ghostscript behind your back as a 'delegate' to process the PDF input) - Using Ghostscript directly
1. Using convert
with -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:
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:
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):
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
.