7

I have taken a screenshot of a figure, cropped it and then inserted it into my document. Now, in the generated pdf file, an unwanted border is visible around the image as shown below.

My code:

\begin{figure}[ht]
    \centering
  \includegraphics[width = 6in, height = 8cm, keepaspectratio]{bilder/relatedMatter/TreeGNG.png}
    \caption{gestaucht}
    \label{Image:TreeGNG}
\end{figure}

Image with unwanted border:

image

Original cropped image:

enter image description here

  • Is it also visible if you zoom in to the border or print it out? maybe the rendering of the pdf viewer is not that good.... – Faekynn Jun 24 '15 at 21:33
  • @Faekynn: Seems like you got the point. They not the actual borders. sometimes they appear only on the top and the left side. Their appearance depends upon the zoom level and cursor position. – skm Jun 25 '15 at 6:55
  • If possible, try another pdf viewer. I'm quite happy with mupdf on linux and SumatraPDF on Win. I encountered many rendering imperfections with the Adobe and Foxit reader... – Faekynn Jun 25 '15 at 11:03
  • @Faekynn: I will try but I use Mac OS. Any particular suggestion for that? – skm Jun 25 '15 at 11:07
  • Unfortunately, I can't help you with Mac OS. But I've found a possibilty to install MuPDF: brewformulas.org/Mupdf But I don't know, what this homebrew is... – Faekynn Jun 25 '15 at 18:16
13

The image is not properly cropped. The margins are still present, they are only made transparent.

Bitmap images

If the original image is a bitmap image, then use a better image editor (gimp, ...) to really remove the margins from the image. Example with the netpbm tools:

pngtopnm -background white -mix <ruJ761V.png | pnmcrop | pnmtopng >TreeGNG-cropped.png

The options -background white -mix set the transparent pixels to white in this image. Then they are removed with the remaining margin of the image by pnmcrop.

Cropped result

In case of a different page color, the white background of the image can be made transparent, e.g. using gimp with its function "Menu → Colors → Color to Alpha":

Cropped result with transparent background

Vector images

If the original image is a vector based format (PDF, EPS), then the conversion to a bitmap image would loose quality. Then tools like pdfcrop are better. Also options viewport or trim with clip can be used to crop the image at TeX level in \includegraphics.

In this case the original PDF file can be found here. The image is on page 3 in Figure 3.

There are lots of ways for the viewport values. They specify the coordinates for the lower left and upper right corner:

viewport=<llx> <lly> <urx> <ury>

The default unit is bp, when no unit is given. TeX units can also be given explicitly. The origin is the lower left corner.

There are lots of ways to get these values:

  • Printing in natural size and measuring with a ruler.

  • Measuring in programs, for example, gv shows the cursor position with coordinates in unit bp.

  • Overlaying the image with a grid (TikZ, package pagegrid, ...).

A separate document helps to experiment. The \fbox shows the new bounding box of the image:

\documentclass{article}
\usepackage[margin=0pt]{geometry}
\usepackage{graphicx}
\pagestyle{empty}

\begin{document}
\setlength{\fboxsep}{0pt}
\setlength{\fboxrule}{0.1pt}
\fbox{%
  \includegraphics[
    page=3,
    viewport=80.5 524 299 684,
    clip,
  ]{901763.pdf}%
}
\end{document}

Result

Alternative with pdfpages:

\documentclass{article}
\usepackage{pdfpages}
\begin{document}
\includepdf[
  fitpaper,
  pages=3,
  viewport=80.5 524 299 684,
]{901763.pdf}
\end{document}

The values can be refined manually or, when the other elements on the page are removed, the test document without \fbox can then be run through pdfcrop.

Then the result can be used directly or the values for viewport can be adjusted.

Refined values, e.g.:

viewport=80.65 524.4 298.55 683.7

In the document the options are used:

\includegraphics[page=3, viewport=80.65 524.4 298.55 683.7, clip]{901763.pdf}

Measuring with grid in TikZ

If no program can be found, which shows the positions nicely, then a grid could be put over the image:

\documentclass[tikz]{standalone}

\newcommand\ImageName{901763.pdf}
\newcommand\ImagePage{3}

\begin{document}
  \begin{tikzpicture}[
    inner sep=0pt, outer sep=0pt,
    x=1bp, y=1bp, % units
  ]
    \node[inner sep=0pt, above right] (image)
      {\includegraphics[page=\ImagePage]{\ImageName}};
    \draw[ultra thin, cyan] (0, 0) grid[step=10] (image.north east);
    \draw[very thin, red] (0, 0) grid[step=100] (image.north east);
  \end{tikzpicture}
\end{document}

Origin is the lower left corner. The cyan lines are 10 bp apart, then red lines 100 bp:

Result grid

Counting the lines, a rough bounding box can be:

left:    80 bp
bottom: 520 bp
right:  300 bp
top:    690 bp

This values can be used to clip the image and increase the resolution of the lines to 1 bp and 10 bp:

\documentclass[tikz]{standalone}

\newcommand\ImageName{901763.pdf}
\newcommand\ImagePage{3}

\begin{document}
  \begin{tikzpicture}[
    inner sep=0pt, outer sep=0pt,  
    x=1bp, y=1bp, % units
  ]
    \node[inner sep=0pt, above right] (image)
      {\includegraphics[page=\ImagePage]{\ImageName}};
    \draw[line width=.05pt, cyan] (0, 0) grid[step=1] (image.north east);
    \draw[ultra thin, blue] (0, 0) grid[step=10] (image.north east);
    \pgfresetboundingbox
    \useasboundingbox (80, 520) (300, 690);
  \end{tikzpicture}
\end{document}

Result finer grid

Now, the origin at the left lower order is at (80 bp, 520 bp) and the cyan lines are 1 bp apart, the blue lines 10 bp.

Then the new bounding box (lines outside the wanted area) is:

left:    81 bp
bottom: 524 bp 
right:  299 bp
top:    684 bp

Rechecking the values find that the left component is a little too large, thus the final bounding box could be:

viewport=80 524 299 684
  • I actually took a screenshot of a pdf file. The screenshot was in .png format. After that, I cropped that png image to get the required image. These borders are not the actual borders, they keep changing their place depending upon your cursor's position. – skm Jun 25 '15 at 6:54
  • @skm Then you should use the PDF page for best quality; pdflatex can include PDF pages, specify the page with option page in \includegraphics and use viewport (or trim) with clip to select the correct area. If you are using dvips, then the PDF page can be converted to EPS, e.g. via pdftops -f <(first) page> -l <(last) page> -eps <PDF file> <EPS file>. – Heiko Oberdiek Jun 25 '15 at 8:21
  • Sorry, I am not that much familiar with all this stuff. Could you please write a short code for it if possible. – skm Jun 25 '15 at 8:22
  • Check the graphicx manual; it will explain these options. – dgoodmaniii Jun 25 '15 at 12:07
  • @HeikoOberdiek: Thanks a lot...super method. Image can come in a very clean way. The only doubt which I have in mind is...how did you decide the parameter for vieport. I mean, how do you know come to know that the parameters should be 80.65 524.4 298.55 683.7 ? Because, with that parameter, I am getting exactly what I needed. – skm Jun 25 '15 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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