# PNG/JPEG to tikz code converter

I was wondering if there is a converter, a program, that lets you upload or create an image and then gives you the tikz code to create it in LaTeX? I found a similar problem here, but none of the answers refer to a converter. I found some converters for equations. However, that is not what I want to convert.

I want to take an image, my signature for example, and make a command \signature that prints my signature in the same size as the text and in the same color. I want to make the command in a .sty file (package) so that I can use the command in all the documents I write, and therefore I do not want a solution intended for one document only.

It's not just my signature I want in this format, but logos as well. I would therefore like a general solution: A converter that converts PNG/JPEG or other image file types to tikz code.

In the signature exaple the code would look something like this:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage}
\RequirePackage{tikz}
\newcommand{\signature}{%
... tikz code ... %
}
\endinput


and:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{mypackage}
\begin{document}
My name is \signature. Now I can easy
sign documents. This document was created by \signature.
\end{document}

• Convert your image to svg (vector graphics) and then use a svg-tikz converter. See e.g. How to export svg to tikz; it also discusses how to obtain svg. Nov 13, 2019 at 12:45
• In addition to what @gernotsaysReinstateMonica is saying, you may also use potrace to convert to vector graphics and inkscape to convert to TikZ. However, AFAIK all the methods will create rather useless TikZ code. Do not expect very simple \draw commands with nice looking coordinates. This raises the question what you want to do with the TikZ code, i.e. why it is better than including the (possibly vectorized) graphics.
– user194703
Nov 13, 2019 at 13:01
• For the .eps format there is the old eps2pgf, but the final result is similar to that described by Schr.'s cat. Nov 16, 2019 at 10:12

You can use the png/jpg image directly and adjust the size and the color to the current font.

The current font size is stored in the macro \f@size which can be used as height attribute for \includegraphics.

The colors of an included image can be modified by specifying the decodearray attribute to \includegraphics. This array contains two multiplication factors for each of the components of a color, one factor for when the component is 0 and one factor for when the component is 1, and interpolated in between. So a black on white RGB image can be converted to green by using the factors 0 1 1 1 0 1 (0% red, which is part of black, stays 0% red, 0% green becomes 100% green, 0% blue stays 0% blue, 100% red (part of white) stays 100% red, 100% green stays 100% green, 100% blue stays 100% blue).

The values for a color can be retrieved with \extractcolorspecs from the xcolor package, for which the argument . is used for the current color (see, e.g., https://tex.stackexchange.com/a/451937/). RGB orange for example is 1,0.5,0. Removing commas and adding a 1 after each component results in the corresponding DecodeArray, here 1 1 0.5 1 0 1. The xstring package can be used for string operations, in this case \StrSubstitute which can change a comma into spaces and a 1 (and adding the final 1 with a \def).

Basic colors in the xcolor package (red, orange, purple, etc.) are defined as RGB so the procedure above works for that.

However, named colors, such as ForestGreen, DarkOrchid, Emerald etc. are defined as CMYK. This is problematic because CMYK DecodeArrays do not work with \includegraphics, but instead they must be inserted directly in the PDF stream, see Changing colors of included cmyk jpg. Also, CMYK is additive (0,0,0,0 is white and 1,1,1,1 is more or less black) so instead of adding a 1 a 0 must be added to keep the white background of the image. Further problem is that you need a CMYK-encoded image file for a CMYK DecodeArray, and PNG does not support CMYK so you need a JPEG. Final problem is that automatic colorspace conversion (for example ImageMagick convert) translates black into 0,0,0,1 instead of 1,1,1,1 (both representations are more or less black) and white into 0,0,0,0. This means that the DecodeArray cannot tell for the CMY components whether the image is black or white at that pixel. To solve this you can do the conversion manually, with the following command line:

convert signature.png -colorspace cmyk -fuzz 10% -fill 'cmyk(255,255,255,255)' -opaque 'rgb(0,0,0)' sigcmyk.jpg


Now all that is left is to check which color model is used for the current color in the document, and use \includegraphics for RGB and the PDF object insertion for CMYK. This check can be done with \ifdefstring from the etoolbox package (needs a bit recent version of this package). Setting the height can also be done directly for the pdf object.

MWE:

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{graphicx}
\usepackage{xstring}
\usepackage{etoolbox}
\def\dfltColorModel{}
\def\dfltColor{}

\makeatletter
\def\signature{%
% get color model and values for current color
\extractcolorspecs{.}{\dfltColorModel}{\dfltColor}%
\ifdefstring{\dfltColorModel}{rgb}{%
% rgb: add 1 after each value to keep white background
\StrSubstitute[0]{\dfltColor}{,}{ 1 }[\colorarray]%
\def\cla{\colorarray\space 1}%
% include signature in current font size and transformed color
\includegraphics[height=\f@size pt, decodearray={\cla}]{signature}%
}{%
% cmyk: add 0 after each value to keep white background
\StrSubstitute[0]{\dfltColor}{,}{ 0 }[\colorarray]%
\def\cla{\colorarray\space 0}%
% output pdf image object directly
\immediate\pdfobj { /DeviceCMYK }%
\edef\deviceobjnum{\pdflastobj}%
\leavevmode%
\pdfximage height \f@size pt attr{/Decode [\cla]}%
colorspace \deviceobjnum%
{sigcmyk.jpg}%
\pdfrefximage \pdflastximage%
}%
}
\makeatother

\begin{document}
\color{orange}
\large
This text is orange and large \signature

\color{ForestGreen}
\Huge
This text is ForestGreen and Huge \signature

\end{document}


signature.png:

Result: