14

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

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?

qubyte
  • 17,299
N3buchadnezzar
  • 11,348
  • 7
  • 55
  • 114
  • 3
    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
  • 1
    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 Ozimica Nov 21 '11 at 15:36
  • 2
    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

3 Answers3

14

Just using the specs found in this Wikipedia page:

\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}

enter image description here

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

enter image description here

egreg
  • 1,121,712
13

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.

enter image description here

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}
percusse
  • 157,807
  • 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
10

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}

Flag

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

qubyte
  • 17,299
  • 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
  • 1
    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
  • Clipping actually doesn't seem to help. I guess the method with an extra rectangle works best. Anyway, clipping would be a sneaky extra rectangle getting rid of any advantage. – qubyte Nov 21 '11 at 16:34
  • Isn't not drawing the white kind of cheating? What if there is something non-white beneath the flag? – qubyte Nov 21 '11 at 17:03
  • 1
    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
  • Nice. TikZ is just as much fun as pgfplots. ;) – qubyte Nov 22 '11 at 03:30