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?
- 103
- 7,642
5 Answers
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.
rem pdf2png.bat
echo off
rem %1 PDF filename without extension
rem %2 density
rem %3 alpha, choose one of {on,off,remove}
del "%~1-*.png"
convert -compose copy -bordercolor red -border 3x3 -density %2 -alpha %3 "%~1.pdf" "%~1-%%02d.png"
Notes:
%1is the first mandatory argument that specifies the filename (without extension) of your PDF to convert.%2is the second mandatory argument that specifies the density. The higher density makes the PNG dimension larger.%3is the third mandatory argument that specifies whether or not you preserve the transparency. Useonif you want to preserve the transparency, otherwise chooseremove. I don't useoffbecause it produces a lousy output.- I added an additional feature such that the output will be enclosed by a red rectangle. If you don't like this feature, remove
-compose copy -bordercolor red -border 3x3from 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.
% myfilename.tex
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-eucl}
\addtopsstyle{gridstyle}{gridlabels=0}
\begin{document}
\begin{pspicture}[showgrid](4,3)
\pstGeonode[
PointName=none,
PointSymbol={x,none,x},
dotscale=2]
(0,0){A}
(1,3){B}
(4,1){C}
\psline(A)(B)(C)
\end{pspicture}
\end{document}
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 both magick and a space because convert has a special meaning in Windows.
- 46,933
- 36,086
-
-
3@Mazzy: Only the image if you put each plot into a separate, compilable TeX input file. Use
standalonedocument class for each plot. – kiss my armpit Jan 12 '13 at 17:35 -
See http://tex.stackexchange.com/questions/91188/shell-script-which-parses-tex-files-for-figures/91203#91203 for one way to extract all the
figureenvironments. The script there could be modified to produce one file per figure. – Ethan Bolker Jan 12 '13 at 17:48 -
Here's a more standard, non-batch version (works for IM 7.0.10-62):
convert -density 200 -alpha on example.pdf example.pngfor 200 DPI and preserving transparency. If using a greyscale, you may want to include-strip(more info). – jvriesem Mar 14 '21 at 15:44
You can automate the process by modifying the externalisation command as in https://tex.stackexchange.com/a/22161 or https://tex.stackexchange.com/a/40795
Here is what I personally would use:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[mode=list and make]
\tikzset{
png export/.style={
% First we call ImageMagick; change settings to requirements
external/system call/.add={}{; magick convert -density 300 -transparent white "\image.pdf" "\image.png"},
% Now we force the PNG figure to be used instead of the PDF
/pgf/images/external info,
/pgf/images/include external/.code={
\includegraphics[width=\pgfexternalwidth,height=\pgfexternalheight]{##1.png}
},
}
}
\begin{document}
{
% Here we specify the figure will be converted and inserted as PNG
\tikzset{png export}
\begin{tikzpicture}
\draw (0,0) circle (1) ;
\end{tikzpicture}
}
% This figure will be inserted as PDF
\begin{tikzpicture}
\draw (0,0) circle (1) ;
\end{tikzpicture}
\end{document}
Because It allows me to convert only the figures that I want to convert. See the first link if you need the conversion to be done for all figures.
- 166
- 582
If you use Texmaker, it offers an option in the document preview window when right-clicking to "Convert page to png image".
TeXstudio doesn't have this option AFAIK. You can still use ImageMagick's PDF to PNG convert (or any other image editor like GIMP):
convert figure.pdf figure.png
Sample code:
\documentclass[preview]{standalone}
\usepackage{tkz-graph}
\begin{document}
\centering
\begin{tikzpicture}
\Vertex[x=-2,y=0]{1}
\Vertex[x=1,y=2]{2}
\Vertex[x=1,y=-2]{3}
\Edge(1)(2)
\end{tikzpicture}
\end{document}
- 339
Acrobat Reader allows for saving a PDF as JPG. Just compile each LaTeX file. Open them in Acrobat and Save As JPG.
- 290
- 4,271
-
1The newest Acrobat Reader (version 2018.011.20055) does not allows for saving a PDF as JPG. Am I wrong something? – Black Mild Jul 24 '18 at 18:14
I wrote a small script, which converts pgf plots to bitmap pictures. I hope it is also useful for some of you. The script creates a temporary latex file, compiles it to a pdf, and then converts it to a bitmap using the convert command.
It can be found here: https://github.com/helgehr/pgf2bitmap
- 11

pgfplots– egreg Jan 12 '13 at 18:02