Graphics, Figures & TablesGet viewport crop coords with overpic grid

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
sdaau
Posts: 26
Joined: Fri Feb 19, 2010 2:08 pm

Get viewport crop coords with overpic grid

Post by sdaau »

Hi all,

I've been messing with this all morning, so I thought I'd post my minimal working example.

Basically, it can get a bit difficult to find the coordinates of a rectangle we want to zoom/crop in Latex, especially since image coordinates are typically expressed in pixels in respect to upper left corner origin (here a GIMP grid view of the attached image, used in the test):
imgtestgrid.png
imgtestgrid.png (102.29 KiB) Viewed 5928 times
whereas the arguments to viewport or trim of \includegraphics are expressed in bp (big points).

So, I thought of using overpic's grid, as a help tool to get approximate coordinates of the desired rectangle to be zoomed/cropped. Overpic was chosen because when it is in absolute mode, you can change easily the units of the grid (mm, pt - any Latex unit). Additionally, overpic's origin of coordinate system is the same as for the viewport argument - bottom left corner.

Basically, this is all very doable - as long as you don't happen to use the width= argument together with grid in overpic - because then the grid will refer to the newly set width, and display the completely wrong numbers ! :!:

So, this minimal working example will basically generate this page:
cropsizetest.jpg
cropsizetest.jpg (253.78 KiB) Viewed 5928 times
and the code is below..

Hope this can help someone,

Cheers!

PS: If the image you're measuring using the grid is too big and you need to move the graphic off-page, you can use something like negative \hspace:

Code: Select all

\hspace{-2in}\begin{overpic}[grid,unit=1bp]{st02softwaregrconv-img/st02softwaregrconv-img002}\end{overpic}
to achieve that.

Code: Select all

% cropsizetest.tex
\documentclass[a4paper,10pt]{article} 
\usepackage[utf8]{inputenc} %support unicode chars direct without crashing :)
\usepackage{graphicx}
\usepackage[abs]{overpic} % must be in abs mode for changing units; else wrong numbers on grid !
\usepackage{hyperref}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\footnotesize

\begin{enumerate}
\item {The png image is 400x200 pixels}
\item {In image coordinate system (origin in upper left corner), the desired rect has x=50,y=50 (px) upper left corner position, and size 150x80 px}
\end{enumerate}

First an overpic ({\em do NOT use ,width=4.6835in in overpic arguments, when using grid in this way - it messes the lengths up!}) - to generate a grid and find coordinates for cropping, for unit=1bp:
\newline

\begin{figure}[!h]
	\begin{overpic}[grid,unit=1bp]{imgtest.png}\end{overpic}% DO NOT use ,width=4.6835in in args here!
\end{figure}

Overpic reports width 400 bp width and 200 bp height (same as pixel sizes).
\newline
In the overpic coord. system (origin in bottom left corner), the bottom left corner of the desired rect should be around x=50,y=70; and its upper right corner at approx x=200,y=150 (bp). Lets try that as viewport=50 70 200 150 ({\em since viewport is also based on origin in bottom left corner coord. system, just as overpic}): 

\begin{figure}[!h]
	\includegraphics[viewport=50 70 200 150,width=1in,clip]{imgtest.png}
	%\includegraphics*[viewport=50 70 200 150,width=1in]{imgtest.png} %alternative with starred version
\end{figure}

Yup, did get the desired rect. \newline
Let's see now what would've happened if we used the ',width' arg in overpic:
\begin{figure}[!h]
	\begin{overpic}[grid,width=2.74in,unit=1bp]{imgtest.png}\end{overpic}% 
\end{figure}

Bad :) 
\end{document}
Attachments
imgtest.png
imgtest.png (47.18 KiB) Viewed 5928 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

BdN3504
Posts: 5
Joined: Wed Sep 14, 2011 2:57 pm

Re: Get viewport crop coords with overpic grid

Post by BdN3504 »

This is extremely time-saving. Thanks a lot for sharing (and sorry for digging up an old thread).
Post Reply