# Crop an image in tikz using 2 points of reference and a height

Consider the following code:

% DOCUMENT TYPE
\documentclass[12pt,a4paper]{article}

% PACKAGES
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[margin=10pt,font=footnotesize,labelfont=bf]{caption}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{fancybox}
\usepackage{amsmath}
\usepackage{array}
\usepackage{makeidx}
\usepackage{graphicx}
\usepackage{floatrow}
\usepackage{subfigure}
\usepackage{enumitem}
\usepackage{appendix}
\usepackage{listings}
\usepackage{multicol}
\usepackage{sistyle}
\usepackage{color}
\usepackage{tikz}
\usepackage{lmodern}
\usepackage{multicol}
\usepackage{seqsplit}
\usepackage[listings,skins,theorems]{tcolorbox}
\usepackage{libertine}
\usepackage[normalem]{ulem}
\usepackage[top=1.25in, bottom=1.25in, left=1.25in, right=1.25in]{geometry}
\usepackage{calc}
\usetikzlibrary{calc}
\usepackage{hyperref}

% PRESENTATION
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\footrulewidth}{0pt}

% DOCUMENT BEGINNING
\begin{document}
\lipsum~{}
\begin{center}
\makebox[\textwidth]{
\begin{tikzpicture}
\node[anchor=north west,inner sep=0] (image) at (0,0) {\includegraphics[width=\paperwidth]{valtech-magna-tiles-tessellation-hexagons-2048x1356.jpg}};
\end{tikzpicture}
}
\end{center}
\lipsum
\end{document}
% DOCUMENT END


Which uses the image available here.

The result will be the following:

Now is the tricky part. I want to cut the image in the following way :

• the top left corner should start from 200px horizontally and 100px vertically from the top left corner ot the original image
• the top right corner should start from 400px horizontally and 100px vertically from the top left corner ot the original image
• the resulting image should be resized to the entire page width
• the image should be cut so that its height is 4cm

Here is the part of the image I want to import (pagewidth here means \paperwidth):

So that the final result will be:

How to do that?

Note: this is an "academic" exercise so I understand how to do that and be able to cut other figures with other lengths/scales in the same way.

# Package adjustbox

Your preamble already contains package adjustbox. It provides the features you need for trimming and clipping

I am not sure, which spacing is needed around the image. The following example lets the image behave as it would have the height of the upper case letter H and the depth of g.

\documentclass[12pt,a4paper]{article}

\usepackage{lipsum}
\usepackage{libertine}
\usepackage{graphicx}
\usepackage[top=1.25in, bottom=1.25in, left=1.25in, right=1.25in]{geometry}
\usepackage{calc}

\begin{document}
\lipsum[7]

\centerline{%
\settoheight{\dimen0}{H}%
Clip={\dimexpr\width*200/2048\relax}
{0pt}
{\dimexpr\width*400/2048\relax}
{\dimexpr\height*100/1356\relax},
width=\paperwidth,
Clip={0pt} {\dimexpr\height-4cm\relax} {0pt} {0pt},
raise=\dimexpr\dimen0-\height\relax,
]{valtech-magna-tiles-tessellation-hexagons-2048x1356.jpg}%
}
\settodepth{\prevdepth}{g}

\noindent
\lipsum[1]
\end{document}


# Pixel sizes with package bmpsize-base

The common TeX compilers do not know anything about the pixel sizes of included images, even if the image format is supported. Project bmpsize uses a (mode-independent) feature of pdfTeX, \pdffiledump, to parse and analyze the binary image bitmap files. Also LuaTeX is automatically supported Via package pdftexcmds. Package bmpsize adds support for image size detection for DVI drivers, unneeded for pdfTeX in PDF mode. But the image width and pixel width detection is outsourced in package bmpsize-base. The following example checks the supported image bitmap formats to get the pixel width and height:

\documentclass[12pt,a4paper]{article}

\usepackage{lipsum}
\usepackage{libertine}
\usepackage{graphicx}
\usepackage[top=1.25in, bottom=1.25in, left=1.25in, right=1.25in]{geometry}

\usepackage{bmpsize-base}
\usepackage{kvsetkeys}
\newcommand*{\imgfile}{valtech-magna-tiles-tessellation-hexagons-2048x1356.jpg}

\makeatletter
\newcommand*{\getpixelsizes}[1]{%
\expandafter\comma@parse\expandafter{\bmpsize@types}{%
\ifbmpsize@ok
\let\PixelWidth\bmpsize@pixelwidth
\let\PixelHeight\bmpsize@pixelheight
\PackageInfo{ImageSize}{%
File = #1\MessageBreak
Size = \PixelWidth\space x \PixelHeight\space pixels\@gobble
}%
\comma@break
\fi
\@gobble
}%
\ifbmpsize@ok
\else
\PackageError{ImageSize}{%
Cannot get pixel sizes for image file\MessageBreak
#1'%
}\@ehc
\fi
}
\makeatother

\begin{document}
\lipsum[7]

\centerline{%
\settoheight{\dimen0}{H}%
\getpixelsizes{\imgfile}
Clip={\dimexpr\width*200/\PixelWidth\relax}
{0pt}
{\dimexpr\width*400/\PixelWidth\relax}
{\dimexpr\height*100/\PixelHeight\relax},
width=\paperwidth,
Clip={0pt} {\dimexpr\height-4cm\relax} {0pt} {0pt},
raise=\dimexpr\dimen0-\height\relax,
]{\imgfile}%
}
\settodepth{\prevdepth}{g}

\noindent
\lipsum[1]
\end{document}


# Without adjustbox

• The trimming can be done by option trim for \includegraphics. Since the unit is pixels, the pixel width and pixel height should be known. The actual calculation can be done by e-TeX's \dimexpr with high accuracy.

• Scaling to \paperwidth can be done by option width.

• The clipping to 4cm can be done by tikz.

Example file:

\documentclass[12pt,a4paper]{article}

\usepackage{lipsum}
\usepackage{libertine}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage[top=1.25in, bottom=1.25in, left=1.25in, right=1.25in]{geometry}

\begin{document}
\lipsum[7]

\centerline{%
\begin{tikzpicture}[inner sep=0pt, baseline=-height("H")]
\clip (0, 0) rectangle (\paperwidth, -4cm);
\node[below right] (img) {%
\sbox0{\includegraphics{valtech-magna-tiles-tessellation-hexagons-2048x1356.jpg}}%
\includegraphics[
trim={\dimexpr\wd0*200/2048\relax}
{0pt}
{\dimexpr\wd0*400/2048\relax}
{\dimexpr\ht0*100/1356\relax},
width=\paperwidth,
]{valtech-magna-tiles-tessellation-hexagons-2048x1356.jpg}%
};
\end{tikzpicture}%
}
\kern-4cm
\nointerlineskip
\vbox to 4cm{\vphantom{g}}

\noindent
\lipsum[1]
\end{document}


• With a so complete solution I'll delete mine. Nice! – Sigur May 17 '15 at 10:49
• Your second example does not compile (missing "}") or wrong sbox0 line? – Vincent May 17 '15 at 18:36
• @Vincent Simple copy&paste error, fixed. – Heiko Oberdiek May 17 '15 at 18:43
• @HeikoOberdiek Last question: how to have the 2048 and 1356 (in the division) directly computed by latex and not put by hand. – Vincent May 17 '15 at 18:45
• @Vincent The updated answer contains a variant with the detection of pixel width and height via package bmpsize-base`. – Heiko Oberdiek May 17 '15 at 19:51