1

(I'm sure lots of ppl have had this exact same problem before, but I could not find it anywhere.)

I have two PNG files, A & B. Image A is like a frame, so it has an empty area in the middle. In this area I want to put some part of image B. I probably do not want to change the "aspect ratio", so same scaling of this part in both dimensions.

enter image description here

So the rectangle of size A,B starting at X,Y in image B should be occupying the 400x600 pixel space at 100,100 in image A (assume distance B = 3/2 * A).

I also want to scale image A arbitrarily. Currently I have (e.g.):

\begin{tikzpicture}
\node {\includegraphics[witdh=0.5\textwidth]{image_a.png}};
\end{tikzpicture}

How can I add the bit from image B (image_b.png) most easily. I have the feeling it should be possible without defining any new commands, or introducing any named variables.

Clarification: I want to be able to change the distances X,Y,A,B easily, so preferably they should each occur exactly once.

More: the cropped and scaled part from image B does not have to fit exactly in the area on image A. Also, the lengths do not have to be pixels. It can be metric or fractions or whatever. I just want a reasonably compact piece of code.

4
  • \llap + \kern + \adjustbox could help. – Cicada Nov 28 '19 at 11:36
  • Yes. Images are boxes, boxes can be overlapped. Try \includegraphics{example-image-a} \llap{\includegraphics[width=0.5\textwidth]{example-image-b}} and see what happens. – Cicada Nov 28 '19 at 11:53
  • \includegraphics does not pay attention to pixels. Do you have the dimensions of image B? – John Kormylo Nov 28 '19 at 15:28
  • Do you have the pixel spacing (which varies from PNG to PNG)? – John Kormylo Nov 28 '19 at 15:36
2

This shows one way to insert a cropped image into a hole in another image.

The saveboxes give us the dimensions of the images in something other than pixels. The scope is used simply to make the clip local. The \fill is there just to show were the hole is.

Note that the clip is defined an terms of the hole in A. All you have to do is move image B around and whatever is over the hole will be cropped to fit.

\documentclass{standalone}
\usepackage{graphicx}
\usepackage{tikz}

\newsavebox{\tempboxa}
\newsavebox{\tempboxb}

\begin{document}

\begin{tikzpicture}% start local definitions
  \savebox{\tempboxa}{\includegraphics[scale=2]{example-image-a}}% measure image A
  \savebox{\tempboxb}{\includegraphics[scale=2]{example-image-b}}% measure image B
  \node[inner sep=0pt] (A) {\usebox\tempboxa};
  \begin{scope}[shift=(A.south west)]
    \clip (0.25\wd\tempboxa, 0.8\ht\tempboxa) rectangle ++(0.5\wd\tempboxa,-0.6\ht\tempboxa);% hole in A
    \fill[red] (A.south west) rectangle (A.north east);% show hole in A (optional)
    \path (0.25\wd\tempboxa, 0.8\ht\tempboxa)% (left, top) of hole in A
      ++(-0.5\wd\tempboxb, -0.5\ht\tempboxb)% (-left, -top) of crop from B
      node[above right,inner sep=0pt]{\usebox\tempboxb};
  \end{scope}
\end{tikzpicture}
\end{document}
1
  • Ok, thanks. I was hoping for something simpler, but this is at least 50% better than what I did myself earlier. – Tomas By Nov 29 '19 at 3:00

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.