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

  • You can clip the image. Would that suffice? – Steven B. Segletes Jun 6 at 9:51
  • I try to avoid that because I will have many of resource files in the same size but they can put an object around. (Otherwise I have to control its X,Y positioning one by one) – ZenithS Jun 6 at 9:53
  • Just curiosity, why do you use a picture and fbox around \includegraphics? You could use \includegraphics inside node contents. – Ignasi Jun 6 at 10:29
  • 4
    the surprising thing is not the line at the bottom, but why are there not lines on all four edges, as you have \fbox around the image. – David Carlisle Jun 6 at 13:28
  • 1
    @Ignasi I print it out to sticker and the artifact was there. – ZenithS Jun 11 at 5:50

Your picture has at the top and at the bottom a transparent background. But the background in the middle is white. What you see is the border. You could try to draw over this border a white line:

\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){%
            \includegraphics[width=\imageWidthMM\Unitmm]{whale}
        };
        \draw[white,line width=2pt] ([xshift=-30.15mm,yshift=-17mm]path picture bounding box.center)rectangle
                   ([xshift=30.15mm,yshift=17mm]path picture bounding box.center); 
    }]  at (0mm,0mm) {};
    \end{tikzpicture}

 \end{document}

But there is imho no guarantee that such artefacts don't appear again at other zooms or when you print.

  • Thanks I have no time to test it yet but that's possible – ZenithS Jun 27 at 7:59

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.

I was curious because I have never had problems and Latex should be able to do this. So I experimented with it and... I don't know why the white background in the middle of your picture is creating those problems, but if you delete it by hand and replace it by transparent background, it seems to work fine at all zoom levels with all pdf viewers I tested. All I did was selecting the white background (fuzzy select tool) and "deleting" it, using GIMP.Try this picture instead: transparent background

You graphics is not actually a proper png image. Check the following for the variants.

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

        \begin{tikzpicture}
    \node[circle,draw=black,line width=0.1mm,minimum size=50mm] at (0,0){};
    \node (img) at (0,0)  {\includegraphics[scale = .2]{tmp1.png}};
    \end{tikzpicture}

    \includegraphics[width=50mm]{tmp.png}

\end{document}

The following image is the one where the edges are cropped using paint. enter image description here

Note that your image had a white background and so the corners are already drawn(thats typically the borders). You can check in the following output of the above code.

enter image description here

  • Thanks I have no time to test it yet but that's possible. – ZenithS Jun 27 at 7:59

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.