4

I want to include only parts of an image. For this, I use the viewport option of graphicx's includegraphics. To find the coordinates, I go by trial and error, adapting the viewport argument until I have it right. This is inefficient.

\documentclass{standalone}
\usepackage{graphicx}
\begin{document}%
\includegraphics[viewport=0 0 10 10,clip]{image}
\end{document}

Is there some way I can easily obtain/draw a grid to efficiently select the coordinates to pass to viewport? For example, when I want to draw on an image using tikz, I draw helplines according to this post. In this case, I don't want to draw at all; I just want to include a specified part of the image only.

gerrit
  • 5,165

2 Answers2

8
\documentclass{article}
\usepackage{graphicx,xcolor}
\usepackage[abs]{overpic}% option [abs] for absolute values 
\begin{document}%

\begin{overpic}[scale=0.5,unit=1mm,grid,tics=10]{tiger}
  \put(50,50){\makebox[0pt]{\colorbox{blue}{\color{white}\Huge Tiger}}}
\end{overpic}

\fbox{\includegraphics[viewport=80mm 120mm 120mm 160mm,scale=0.5,clip]{tiger}}

\end{document}

enter image description here

The absolue values are scaled down, the reason why I had to double the coordinates for the eye.

  • Perhaps I did not phrase my question clearly. The coordinates in this figure do not correspond to the coordinates I need to pass to viewport to get my desired subset of the image; for example, suppose I want to include only the right eye. \includegraphics[viewport=39 60 44 64,clip]{tiger} does not give me the right eye, but a different (much smaller) area. – gerrit Apr 29 '13 at 18:38
  • I set the unit to mm and scaled the image to 0.5! –  Apr 29 '13 at 18:43
  • I find it hard to reproduce it with my local example here. I might be still doing something wrong. Does it make a difference in what format the picture is? – gerrit Apr 29 '13 at 18:55
  • no, the picture doesn't matter. Can you put it anywhere for a download? However, try it first with my example. –  Apr 29 '13 at 18:56
  • Looks like I was missing the abs option (I think it wasn't there in an earlier revision), but that one's pretty essential. Got it now! – gerrit Apr 29 '13 at 19:13
  • If you use a scale=0.5 you should consider to also use unit=0.5mm to get the same values on the grid axes that you need to put as the viewport later on. – moooeeeep Feb 14 '20 at 11:02
1

With PSTricks.

\documentclass[preview,border=12pt]{standalone}% remove border option to get a tight output
\usepackage{pstricks}

\def\M{10}% columns \def\N{10}% rows \def\scale{1}% scale \def\filename{shaolin}% filename

\usepackage{graphicx} \newsavebox\IBox \savebox\IBox{\includegraphics[scale=\scale]{\filename}}

\addtopsstyle{gridstyle} { gridcolor=yellow, subgridcolor=gray, subgriddiv=10, griddots=0, subgriddots=5, gridwidth=0.4pt, subgridwidth=0.2pt, }

\psset { xunit=\dimexpr\wd\IBox/\M, yunit=\dimexpr\ht\IBox/\N, }

\def\Navigator{% \begin{pspicture}showgrid=top \rputbl{\usebox\IBox} \end{pspicture}}

\def\ViewPort(#1,#2)(#3,#4){\viewport(\the\dimexpr#1\psxunit,\the\dimexpr#2\psyunit)(\the\dimexpr#3\psxunit,\the\dimexpr#4\psyunit)}

\def\viewport(#1,#2)(#3,#4){\includegraphics[scale=\scale,viewport=#1 #2 #3 #4,clip]{\filename}}

\begin{document} %\Navigator% disable it after using \ViewPort(2,3)(6,6) \end{document}

How to use:

  1. \Nagivator will show the grid from which we determine the viewport coordinates.

    enter image description here

  2. \ViewPort is used to clip everything outside viewport.

    enter image description here

  3. Remove border option in the document class to get a tight output.