# Convert PNG image to TikZ code

I am trying to convert this image here into a somewhat clean TikZ code

Now I downloaded potrace, and tried to convert the image to an eps, and then to use inkscape to convert it into TikZ code. Alas, potrace does not work for me. Either that or I can't make it work, it seems like it's rather complicated. Opening up the command window and typing in the path renders nothing. Anyway, does anyone here have any nice methods for converting images into TikZ code?

-
IMHO, there is no need to convert images into automatic generated TikZ code. You won't really be able to change it afterwards and the final result of the tikzpicture is a PDF or EPS anyway, so why not convert the image to one of these formats using potrace or a similar software? In this example it would be much simpler coding it by hand, anyway. – Martin Scharrer Nov 21 '11 at 15:35
I know you are explicitly asking for a way to convert an image to TikZ, but: why, instead of converting, don't you try to create the flag by yourself? It's just a red rectangle, and over it two white rectangles and two dark blue rectangles... – Nicolás Nov 21 '11 at 15:36
You find the specifications here: it should be quite easy to turn them into a TiKZ picture. – egreg Nov 21 '11 at 15:36
Why are you using potrace? Inkscape has a trace utility built in Path -> Trace Bitmap.... – qubyte Nov 21 '11 at 16:20

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\definecolor{RedNo}{HTML}{EF2B2D}
\definecolor{BlueNo}{HTML}{002868}

\begin{document}
\begin{tikzpicture}[scale=.5]
\fill [RedNo]  (0,0) rectangle (6,6) ;
\fill [RedNo]  (0,10) rectangle (6,16) ;
\fill [RedNo]  (10,0) rectangle (22,6) ;
\fill [RedNo]  (10,10) rectangle (22,16) ;
\fill [BlueNo] (7,0) rectangle (9,16) ;
\fill [BlueNo] (0,7) rectangle (22,9) ;
\end{tikzpicture}
\end{document}


With the code

\definecolor{GreenIt}{HTML}{009246}
\definecolor{BWhiteIt}{HTML}{F1F2F1}
\definecolor{RedIt}{HTML}{CE2B37}

\begin{tikzpicture}[scale=2]
\fill [GreenIt]  (0,0) rectangle (1,2) ;
\fill [BWhiteIt] (1,0) rectangle (2,2) ;
\fill [RedIt]    (2,0) rectangle (3,2) ;
\end{tikzpicture}


one gets another beautiful flag

-
You could simplify a little by using rectangle, e.g. \fill [GreenIt] (0,0) rectangle (1,2); – Torbjørn T. Nov 21 '11 at 16:16
@TorbjørnT. Just looking at rectangle syntax. :) – egreg Nov 21 '11 at 16:17
Look at my example for them. ;) – qubyte Nov 21 '11 at 16:20
If you put the rectangles in the right order, you need only specify five of them, rather than six. (This could turn into code golf) – Seamus Nov 21 '11 at 16:24
Oh my god! Waves arms frantically 45 minutes. 5 rectangles. It's been done! :P – qubyte Nov 21 '11 at 16:26

I was bored and tried something that I saw in the manual. It is the double line example on manual p.160 : It is as if the blueish part was making a sharp cornered infinity sign outside the clipped part. So I used (with egreg's nice colors)

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\definecolor{RedNo}{HTML}{EF2B2D}
\definecolor{BlueNo}{HTML}{002868}
\begin{document}
\begin{tikzpicture}
%\clip (0,0) rectangle (22mm,16mm);
\fill[RedNo] (0,0) rectangle (22mm,16mm);
\draw[smooth cycle, draw=white,double=BlueNo,line width=1mm,double distance = 2mm]
(8mm,8mm) -- (24mm,8mm) -- (24mm,18mm) -- (8mm,18mm)--%
(8mm,-2mm) -- (-2mm,-2mm) -- (-2mm,8mm) -- cycle;
\end{tikzpicture}
\end{document}


This is what you get.

Basically, the whole trick is the smooth cycle option. If you uncomment line in between you get the original flag. :)

EDIT : After having fun with the loops, as Torbjørn T. quickly noticed, one can also directly put two strokes of double line over a red rectangle without doing the loop around it.

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\definecolor{RedNo}{HTML}{EF2B2D}
\definecolor{BlueNo}{HTML}{002868}
\begin{document}
\begin{tikzpicture}
\fill[RedNo] (0,0) rectangle (22mm,16mm);
\draw[draw=white,double=BlueNo,line width=1mm,double distance = 2mm]%
(8mm,0) -- (8mm,16mm) (0,8mm) -- (22mm,8mm);
\end{tikzpicture}
\end{document}

-
How about just \draw[draw=white,double=BlueNo,line width=1mm,double distance = 2mm] (8mm,0) -- (8mm,16mm) (0,8mm) -- (22mm,8mm);? – Torbjørn T. Nov 21 '11 at 22:00
@TorbjørnT. Perfect. I just wanted to make a fancier argument :D Let me add that too for the sake of completeness. – percusse Nov 21 '11 at 22:04

The nicest interface for tracing I've used is through Inkscape. Note that you don't need to use potrace first. Inkscape has a built in tracing tool, and can even output TikZ if you have the plugin for it. However, the image above is pretty straightforward to produce in TikZ just by hand coding it.

For example, a (very) rough version of the flag can be coded with only rectangles:

\begin{tikzpicture}[scale=0.5]
\path[fill=red!90!black] (0,0) rectangle (22,16);
\path[fill=white] (6,0) rectangle (10,16);
\path[fill=white] (0,6) rectangle (22,10);
\path[fill=blue!40!black] (0,7) rectangle (22,9);
\path[fill=blue!40!black] (7,0) rectangle (9,16);
\end{tikzpicture}


Wikipedia gives the correct proportions and colours, so you can code it in really simply.

-
Sorry Mark! Somehow I missed your answer. I've deleted mine, since it contained nothing that isn't in yours and egreg's answers. – Seamus Nov 21 '11 at 16:26
Hahaha. I thought I was going mad! – qubyte Nov 21 '11 at 16:27
The red is spilling out; I think that drawing six rectangles is better for avoiding rounding problems. – egreg Nov 21 '11 at 16:28
Probably, but it is quick and dirty. I'd be tempted to make it epsilon larger and then clip it back. – qubyte Nov 21 '11 at 16:30
Now to "improve" this answer, I just made a white rectangle around the flag. That took care of the edges =) – N3buchadnezzar Nov 21 '11 at 20:56