I have created various plots using the pgfplots
package for my thesis. Now I would export them in jpg or png format. Does it exist a way to achieve this purpose?
[Tex/LaTex] How to save a figure produced by tikz save/export as JPG/PNG file
jpegpgfplotspngtikz-pgf
Related Solutions
LaTeX graphics packages
LaTeX and its graphics packages do not touch the image data. TeX does not even provide the reading of binary data. Thus LaTeX passes the image as file name reference to the driver. Also most of the drivers are not image processing programs. They only move the image data in a form appropriate for the output format. For example, dvips only copies the PostScript file into the output PostScript file. Also pdfTeX often do not need to unpack the image data and can copy the data to the PDF structures. Some PNG files are uncompressed and compressed. But this process does not change the image data.
Driver issues Very view drivers are able to resize an image, AFAIK Acrobat Distiller and GhostScript does some obscure things with images like using JPEG compression for PNG images. I do not know how this can be turned off. The related options given in Ps2pdf.htm seems to have no effect.
But you are using pdflatex that does not change the image data. This can be tested. Take a PNG image and convert it to PPM. Also embed the image in a LaTeX document and run pdflatex. The program pdfimages (xpdf) extract the image in PPM format.
convert image.png image.ppm
pdflatex test.tex
pdfimages test.pdf t
diff t-000.ppm image.ppm
The files t-000.ppm
and image.ppm
should be identical, if the PPM format
is the same (there is a binary and an ASCII variant).
** Different viewing programs** However the programs to view the images are different. The image viewer and the PDF viewer are usually different programs that uses different methods for viewing. For example, a program might use anti-aliasing, …
Scaling vs. resizing
There are no problems in scaling images in LaTeX independent of its method:
- Option
scale
- Options
width
,height
\scalebox
- …
Only the place that the image uses on the page differs, the image data remains the same.
A different term is the resizing of an image. Then the actual number of pixels change. Of course, an image processing program cannot invent missing details if the image is enlarged. It can only use better or worse methods to limit the artefacts of resizing.
Screenshots
Screens have low resolutions comparing to printed media and are usually stupid bitmap images of the pixels on the screen even if the original data were high quality vector data (non-pixel fonts, vector drawings).
Some hints to get better quality:
- In some cases a vector screenshot program might be available, e.g. gtk-vector-screenshot.
- A special case are web pages. They could be converted to PDF by printing or there are programs/sites that perform the conversion. But caution, PDF, especially PDFs from screenshot programs might contain bitmap data instead.
- Higher screen resolution with larger font/symbol settings or using settings for visually impaired people.
- A large monitor helps that allows large windows for the screenshot programs that can only catch the pixels inside the screen.
- And it can make sense to turn off anti-aliasing and similar (ClearType) to get clean pixel data. Thus that a black line is displayed by black pixels and not by many gray levels at its edges. That makes it easier to optimize the image for the final media.
You could use the preview
package to create a single page from every tikzpicture
environment using the \PreviewEnvironment
(or similar, see the manual) macro. The resulting PDF can then be converted to multiple PNGs if required (but I suggest keeping the diagrams vector graphics!). However, this will only number all images and does not allow you to label them. You will run into trouble if the order or number of tikzpictures
changes.
Best Answer
First of all, make sure you have installed ImageMagick as the following code uses ImageMagick's
convert
command.Once you have had a PDF output, you need to convert it to PNG by using the following batch file named
pdf2png.bat
. It is convenient to register the batch path to the system variable.Notes:
%1
is the first mandatory argument that specifies the filename (without extension) of your PDF to convert.%2
is the second mandatory argument that specifies the density. The higher density makes the PNG dimension larger.%3
is the third mandatory argument that specifies whether or not you preserve the transparency. Useon
if you want to preserve the transparency, otherwise chooseremove
. I don't useoff
because it produces a lousy output.-compose copy -bordercolor red -border 3x3
from the code above.Exercise
It is just an example. Your scenario in which you get a PDF might be different from mine. My scenario is as follows: compile the following input file with
latex->dvips->ps2pdf
to get a PDF output.You can invoke the batch from the editor of your choice, but here I invoke the batch from the DOS prompt:
The output is:
The red rectangle is the border produced by
-compose copy -bordercolor red -border 3x3
.Attention!
For Windows users, the
convert
alone no longer works. Instead it must be preceded by bothmagick
and a space becauseconvert
has a special meaning in Windows.