[Tex/LaTex] \includegraphics with transparent

graphicsincludegraphicstikz-pgftransparent

Description

I wanted to create a PDF with resource images (square).
With this resource image (See below), it contains transparent space. I use \includegraphics to include an image.

But when I compile, it contains an edge line (below the dolphin) between transparent and actual images appeared on the output. (See image)

Questions

How can I remove this edge or import in without it.

Tex (MikTex / xetex)

 \documentclass{article}
 \PassOptionsToPackage{cmyk}{xcolor}
 \usepackage{pst-all}
 \usepackage{graphicx}
 \usepackage{grffile}
 \usepackage{tikz}
 \usepackage[paperheight=69.1mm,paperwidth=69.1mm,margin=0mm]{geometry}
 \newcommand{\imageWidthMM}{60.3}
 \newcommand{\Unitmm}{mm}
 \newcommand{\imageWidthPTx}{171.57011811736}
 \newcommand{\imageWidthPTy}{175.07011811736}

 \begin{document}
    \hspace*{-5.32mm}%
    \begin{tikzpicture}
    \node [circle, draw= black,line width=0.1mm, minimum size=69mm] at (0mm,0mm) () {};
    \node[circle,
    text=white,
    minimum size=60mm,
    path picture={
        \node at (path picture bounding box.center){
            \begin{picture}(\imageWidthPTx,\imageWidthPTy)
            \fbox{\includegraphics[width=\imageWidthMM\Unitmm,angle=0]{**PATHTOIMAGES**}}
            \end{picture}
        };
    }]  at (0mm,0mm) {};
    \end{tikzpicture}

 \end{document}

Resources file

Resources

The output PDF

Output PDF

Best Answer

Solution

This is possible by calling ImageMagick within TeX (simplified MWE):

\documentclass{standalone}
\usepackage{graphicx,tikz}
\immediate\write18{convert whale.png -transparent white tmp.png}
\begin{document}
  \begin{tikzpicture}
    \node[circle,draw=black,line width=0.1mm,minimum size=70mm] at (0mm,0mm){};
    \node(img){\includegraphics[width=70mm]{tmp.png}};
  \end{tikzpicture}
\end{document}

Compile

I compiled the document (tmp.tex) using:

pdflatex --shell-escape tmp.tex

where:

  1. the flag --shell-escape allows pdflatex to call a system command (see 2.)
  2. \write18 executes its argument as a system command; \immediate makes sure this happens before \includegraphics requests the result (more info here)
  3. convert invokes ImageMagick and replaces all white in whale.png with -transparent, writing the output to tmp.png

Result

enter image description here

Linux vs Windows (MikTeX) issues

  • on Windows: \write18 must specify the full path to ImageMagick, which typically looks like: "C:/Program Files/ImageMagick-7.0.8-Q16/magick.exe" instead of convert
  • using MikTex: the flag to allow \write18 is --enable-write18 instead of --shell-escape
  • using MikTex: \write18 is blocked from running programs like magick.exe. There are supposedly options to bypass this security feature (see here) but none of them worked for me...

... as a result, I'm unable to reproduce the solution on Windows.