# How to crop background from image automatically?

I have a Google Docs drawing that is a small scheme. However, this small scheme is surrounded by a lot of background that occupies a lot of useless space.

I do not want to remove the background manually because I may need the extra space in the future.

I know Latex has a lot of image packages, so I was wondering if there is some package that I can use to automatically detect that useless background and crop my image so I do not have all that white space around it.

I know some of you will suggest the external tool pdfcrop. I have experimented with it, but it does not crop the image at all.

• You can try pdfcrop if it is a pdf. Gimp or imagemagick or ghostscript if it is an image. – user11232 Mar 16 '14 at 23:11
• Can you give me a MWE with an image in PDF format (using pdfcrop)? I am quite new at this :S – Flame_Phoenix Mar 16 '14 at 23:38
• For which one?. In gimp simply open it and try to chop off. For pdfcrop it is pdfcrop file.pdf filecropped.pdf and for imagemagick convert -trim file.png file-trimmed.png from command prompt. – user11232 Mar 16 '14 at 23:42
• Ahh, so Latex has no packages that can do this automatically for me? I have to do it manually using an external tool? – Flame_Phoenix Mar 16 '14 at 23:44
• See the answer to tex.stackexchange.com/q/151646/3954 for a way to use pdfcrop from within you document. – Gonzalo Medina Mar 16 '14 at 23:45

You do not need a cropped copy with a external program, only add some options to \includegraphics. This MWE show the same image twice (renamed to image.png) , with and without the useless background. Both images are inside a framed box to show the edges:

\documentclass{article}
\usepackage{graphicx}
\begin{document}

\section*{Original image}

\fbox{\includegraphics[width=\linewidth]{image.png}}

\section*{Trimmed and clipped image}

\fbox{\includegraphics[width=\linewidth,trim=6.5cm 6cm 6.5cm 4cm,clip]{image.png}}

\end{document}


Edit: Since the goal seem to be the automatic cropping more that avoid a cropped copy, this has been perfectly solved in the comments (with an external tool) as far I know, so only put in practice to in a MWE.

The following code with a poorman's macro added to the above MWE:

\section*{Automatic crop}
Note: compile with \verb|--shell-escape|\\

\newcommand\cropped[1]{%
\immediate\write18{convert -trim #1.png #1cropped.png}%
\includegraphics[width=\linewidth]{#1cropped.png}}

\fbox{\cropped{image}}


Will produce this image automatically:

Really the cropping was not make really by LaTeX and neeed one extra file, but who cares? Anyway is done while running pdflatex.

• Yes but, can this trim be done automatically by an algorithm? – Flame_Phoenix Mar 17 '14 at 11:02
• @Flame_Phoenix Do you mean like the Autocrop or Zealous Crop commands in Gimp? This mean pattern recognition and image recognition, that is, analyze the bitmaps and vector images. I am afraid that this is asking too much for a text typographic system. But who know, I am still learning ... :) – Fran Mar 17 '14 at 11:31
• yes exactly like autocrop in gimp. I heard that this is in fact possible, and that one can use pdfcrop to achieve that, but I am not sure how to do it :S – Flame_Phoenix Mar 17 '14 at 12:27
• @Flame_Phoenix I added an example using convert and the png image. I cannot test now, pdfcrop, but should be nearly the same, although may be cropping only the text margins, not the blank areas of a included bitmap. But you convert to PNG and -trim at the same time. (convert -trim image.pdf image.png). – Fran Mar 17 '14 at 13:18
• Alright ... how do I run this with --shell-escape using Kile? xD – Flame_Phoenix Mar 17 '14 at 13:40