Home > graphics > How to trim / clip / crop graphics without trial and error?

# How to trim / clip / crop graphics without trial and error?

December 10Hits:3

Often I include graphics with the trim option. Everytime I have to start a trial and error search for the right values. Is it possible to do it more straight forward?

Sometimes the grid in the corner is to far away, when cropping to axis labels. So I made an update, where I add rectangles over the whole image. And I add an optional parameter to control how deep the rectangles are drawn.

The thick lines have a distance of 10mm, the thin ones of 2mm. This is independent of the image if no width or height argument is passed to the image.

\usepackage{tikz}

% Linen über Graphiken
\newcommand{\showgrid}[3][5]{%
\providecommand{\griddepth}{#1}
\resizebox{#2}{!}{%
\begin{tikzpicture}[inner sep=0]
\node[anchor=south west] (image) at (0, 0) {#3};
% Linien einfügen
\begin{scope}[red]
% Äußere Schleife für dicke Rechtecke
\foreach \iThick in {0, ..., \griddepth} {%
\path (image.north east) ++(-\iThick, -\iThick) coordinate(topright);
\draw[semithick] (\iThick, \iThick) rectangle (topright);
% Zwischen den Linien auffüllen
\ifnum\iThick<\griddepth
% dünne Rechtecke
\foreach \iThin in {1, ..., 4} {%
\path (image.north east) ++(-\iThick, -\iThick) ++(-\iThin/5, -\iThin/5) coordinate(topright);
\draw[very thin] (\iThick, \iThick) ++(\iThin/5, \iThin/5) rectangle (topright);
}
\fi
}
\end{scope}
\end{tikzpicture}
}
}



Call the macro with

\showgrid[6]{0.9\linewidth}{\color{lightgray}{\rule{20cm}{25cm}}}



On images it is still necessary to put the width from the image to the first argument of \showgrid.

\showgrid[1]{0.8\linewidth{\includegraphics[clip, trim=20mm 34mm 8mm 16mm]{Test.pdf}}



To speed up the trimming process when including an image I build a little script with the help of TikZ:

% Gitter über Graphik
\newcommand{\showgrid}[2]{%
\newcommand{\gridlen}{5}
\resizebox{#1}{!}{%
\begin{tikzpicture}[inner sep=0]
\node[anchor=south west] (image) at (0, 0) {#2};
% Koordinaten fast oben rechts
\path (image.north east) -- ++(-\gridlen, -\gridlen) coordinate (obenrechts);

\begin{scope}[red]
\draw[xstep=.2, ystep=.2, very thin] (0, 0) grid (\gridlen, \gridlen);
\draw[xstep=1, ystep=1, semithick] (0, 0) grid (\gridlen, \gridlen);
% Gitter oben rechts
\draw[xstep=.2, ystep=.2, shift={(obenrechts)}, very thin] (0, 0) grid (\gridlen, \gridlen);
\draw[xstep=1, ystep=1, shift={(obenrechts)}, semithick] (0, 0) grid (\gridlen, \gridlen);

% Rahmen
\draw (0, 0) rectangle (image.north east);
\end{scope}
\end{tikzpicture}%
}
}



One can call the script by

\showgrid{0.8\linewidth}{\rule{10cm}{10cm}}



or

\showgrid{0.8\linewidth}{\includegraphics[clip, trim=31mm 58mm 102mm 31mm]{Test.pdf}}



In the second call, one has to shift the width from includegraphics to the first argument of \showgrid.

The thick lines in the grids stand for 1cm, the thin ones for 2mm. These units are independent from the width of the graphic, because it is scaled after the grids are added. This is the reason why the argument of width=... must be shifted from includegraphics to the first argument of \showgrid.

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}}

{
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](\M,\N)
\rput[bl](0,0){\usebox\IBox}
\end{pspicture}}

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

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

\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.

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

3. Remove border option in the document class to get a tight output.
Tags:graphics, crop

## Related Articles

• ### How to trim / clip / crop graphics without trial and error?December 10

Often I include graphics with the trim option. Everytime I have to start a trial and error search for the right values. Is it possible to do it more straight forward? --------------Solutions------------- Sometimes the grid in the corner is to far awa

• ### What is the best way to clip a graphic to a region?February 19

What is the best way to clip a graphic to a certain region? Here's a very simple implementation to show what I mean: Show[ CountryData["World", "Shape"], Graphics[{White, FilledCurve[{{Line[{{-200, -100}, {200, -100}, {200, 100}, {-200

• ### Graphics Driver - Low Grapich Error May 24

Note: This is an attempt to create a canonical question that covers all instances of "low-graphics mode" error that occurs to a user, including but not limited to installation of wrong drivers, incorrect or invalid lightdm greeters, low disk spa

• ### Develop for iPad with HTML5: Trial and ErrorJune 23

At Apple's Worldwide Developer Conference (WWDC) last week, Apple CEO Steve Jobs told developers that it supported two development platforms for its various iDevices: the App Store, and web applications written with HTML5, CSS, and JavaScript. Though

• ### Learning by doing (and programming by trial and error) November 21

How do you learn a new platform/toolkit while producing working code and keeping your codebase clean? When I know what I can do with the underlying platform and toolkit, I usually do this: I create a new branch (with GIT, in my case) I write a few un

• ### How can I calculate settings for manual flash to reduce trial and error? April 9

This question already has an answer here: How can I calculate the effect of non-TTL flash on exposure? 2 answers I have two Yongnuo 560-III flashes that I have been using for a while, In order to get good results I am having to do a lot of trial and

• ### Using Youtube editor to trim clips longer than 15 minutesOctober 4

I wanted to transfer some videos across to YouTube the other day and forgot two of them went over the 15 minute limit. So they were rejected, no problem with that (ended up splitting the clips then uploading once more). Then today I stumbled across Y

• ### PDF Viewer with Trim Margins/Crop Feature August 23

The application Goodreader on my iPad has a feature called "crop tool", which comes very handy for reading scientific papers. The following page seems to describe the feature quite well: http://www.practicallyefficient.com/2011/11/15/fat-pdf/ I

• ### Clip Cropping in Inkscape Produces only a Blank OutlineNovember 26

In Inkscape, on OS X, I select two objects, one of which is an intended clipping or cropping mask. The object that is intended to do the clipping is higher up (above the base .png), I then select Object->Clip->Set and the image disappears, leaving o

• ### Overflow converting ratio denominators (17 :18) - Clip croppedDecember 10

I have a clip with 59.997fps framerate and a composition with 60fps framerate. In order to correctly import and use the clip I have to re-interpret the clip by right-clicking on the clip file and Interpret footage > Main and set the right framerate..

• ### How to crop graphics outside PlotRange with Show? May 15

This question already has an answer here: Is it possible to induce Mathematica to chop off parts of Graphics objects that exist outside of a Frame? 2 answers Is there an option or method to prevent Graphics from being displayed outside the PlotRange

• ### Trim/Clip scope by a defined curve September 25

Hy I have to plot a vector field inside region in tikz. The arrows of the vector field should not appear outside the region. The region is the interior of : \draw [black, fill=white] plot [smooth cycle] coordinates {(ar) (br) (cr) (dr) (er)}; Thanks

• ### Sharepoint 2010 Trial ended error after upgrade to licenced productAugust 13

I have a sharepoint 2010 installation that I have been using on a trail version while my boss orgnised a movl subscribtion which would include sharepoint. Thats done now and last week the activation key was put into sharepoint. This did work as i cou

• ### Ubuntu @myLaptop will boot only when failsafe graphics mode selected(several errors) September 15

Possible Duplicate: "You do not appear to be using the nvidia x server"(screenshot included) Suddenly cannot boot into Ubuntu. I am running ubuntu 11.04 at my asus laptop. Laptop specs: CPU: i5-2410M @ 2,3GHz RAM: 4GB GPU: nvidia gforce GT 520M

• ### Clipping Rasters with a mask layer errorNovember 1

I've got a NDVI layer that I'm trying to clip with a vector file but when I try and use GDAL Tools clipper I get an error. That shows up like this: What am I doing wrong? This is before I even pick the mask layer! Traceback (most recent call last): F

• ### Custom TransformationFunction in Graphics? GeometricTransformation::nonaffine errorJune 18

There are many functions in Mathematica, which return expressions with TransformationFunction head, for example RotationTransform. Unfortunately, there is no PerspectiveTransform amongst them. How to create custom transformation functions in general?

• ### QGIS 2.4 graphical modeler add field: error executing algorithmJanuary 21

I have to add more than 20 fields to a lot of shapefiles. I used to do it with the tablemanager plugin. Beside it is time consuming, misstakes can be made. So I want to automate that procedure through the graphical modeler. I tested it with adding 2

• ### Scaling a graphics leads to an error October 20


• ### Nested trial and error in if-else conditionFebruary 8

Using Rails 3.2, I have the following: g = Geocoder.search(address) # if no address found, geocode with latitude and longitude if g.empty? g = Geocoder.search(latitude, longitude) if g.empty? log.info "Geocode failed for #{latitude}, #{longitude}&quo

• ### How, besides trial and error, can I know which VirtualHost Apache 2.4.10 on Debian is loaded first?December 7

After (or rather, during a migration between web hosts because I want to keep root on my VPS) I was gently informed that my homepage was "i tried to open the site, but it opened an index site, not a normal website". I went to see what the visito