3

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

cfr
  • 198,882
ZenithS
  • 31
  • You can clip the image. Would that suffice? – Steven B. Segletes Jun 06 '18 at 09: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 06 '18 at 09:53
  • Just curiosity, why do you use a picture and fbox around \includegraphics? You could use \includegraphics inside node contents. – Ignasi Jun 06 '18 at 10:29
  • For a pdf with images, may be you want to look at incgraph – Ignasi Jun 06 '18 at 10:30
  • 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 06 '18 at 13:28
  • A non-TeX solution (at least on Ubuntu) could be to use convert with the -flatten option on the command line. This replaces transparent pixels with white pixels. A full command would be convert -flatten <input>.png <output>.png. – Max Jun 06 '18 at 14:11
  • Remove the \fbox. – Ulrike Fischer Jun 06 '18 at 14:20
  • @DavidCarlisle I see top and bottom lines, even without \fbox, just with \includegraphics inside the node. – Ignasi Jun 06 '18 at 14:44
  • @Ignasi They're just artefacts, though. If I zoom in, they disappear. Or if I zoom out, also they disappear. – cfr Jun 07 '18 at 01:49
  • @DavidCarlisle If you look to the resource image you will found out that is square and has transparent on top and bottom.

    The lines are edges between them.

    – ZenithS Jun 11 '18 at 04:34
  • 1
    @Ignasi I print it out to sticker and the artifact was there. – ZenithS Jun 11 '18 at 05:50
  • @ZenithS I don't know if the lines are artifacts. A file with just \includegraphics{whale} show top and bottom lines. I can see them in TeXworks pdfviewer, Sumatrapdf and AdobeReader. Sometimes top and bottom lines are shown, sometimes only one of them, sometimes they desappear and on a larger zoom they appear again. I think this is a problem with the original image. – Ignasi Jun 11 '18 at 06:53
  • I also provide the raw images there, and it's look ok to me – ZenithS Jun 18 '18 at 10:22
  • If the problem is with the transparent part of the image, could you just flatten them all in one go with ImageMagick? – David Purton Jun 18 '18 at 13:25
  • Something like: mogrify -flatten /path/to/resources/*.png This removes the lines in the whale image for me. – David Purton Jun 18 '18 at 13:31
  • Using transparent pngs in pdf documents is not only a problem in LaTeX. I get the same problem when I import the image from the question in a LibreOffice Writer document and export as pdf. See also https://stackoverflow.com/questions/35182147/tcpdf-gray-outline-on-transparent-png. – Marijn Jun 18 '18 at 15:15
  • I am not sure why, but I could not reproduce your error, it works on my laptop. – zyy Jun 18 '18 at 15:37
  • @ZenithS - "I will have many of resource files in the same size but they can put an object around." Can you clarify your overall objective here? As some people have suggested, there may be non-TeX solutions to automating background removal, which may solve your problem, as nox has shown, and I can confirm. – jessexknight Jun 20 '18 at 01:11
  • The best solution as suggested before is to flaten your images before to use them. But if you don't want that, I can't see why, you can remove your {picture} environment and your fbox and add the options [...,trim=0mm 70mm 0mm 70mm, clip] to \includegraphics. The whale is at the same position and with the same size, so no difference, but without the lines. – Kpym Jun 20 '18 at 06:12

4 Answers4

5

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.

jessexknight
  • 2,732
4

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.

cfr
  • 198,882
Ulrike Fischer
  • 327,261
2

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

nox
  • 4,160
  • 12
  • 26
1

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

David
  • 1,964