18

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.

enter image description here

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.

Here is the png image file and here is the pdf version downloaded with Gdocs.

  • 2
    You can try pdfcrop if it is a pdf. Gimp or imagemagick or ghostscript if it is an image. –  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
  • 1
    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. –  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
  • 1
    See the answer to http://tex.stackexchange.com/q/151646/3954 for a way to use pdfcrop from within you document. – Gonzalo Medina Mar 16 '14 at 23:45
  • Well, graphicx can do this but you have to decide from where to clip. On the other hand you can write a batch file or bash script. If you are interested in batch file let me know. –  Mar 16 '14 at 23:46
  • @HarishKumar: How would this batch script work? Could I then use it with latex so I dont have to run it manually every time I add a new image? – Flame_Phoenix Mar 17 '14 at 00:13
  • pdfcrop tool does not seem to work at all :S I have added the png image and the pdf file for future refrence. – Flame_Phoenix Mar 17 '14 at 00:13
  • pdfcrop needs perl. The answer linked by Gonzalo may be a best shot for you. –  Mar 17 '14 at 00:19
  • @HarishKumar: The reason I found out that pdfcrop does not work at all its because I experimented the solutions provided in the link he gave me :P – Flame_Phoenix Mar 17 '14 at 00:41
  • You have to compile that example with --shell-escape –  Mar 17 '14 at 00:43
  • I don't understand the point about needing the space in the future at all. Why don't you just make a copy of the image? – cfr Mar 17 '14 at 04:11
  • @cfr: if I wanted to manually create a copy, or manually trim the images myself, I would not be asking this question here :S I am looking for an automatic way to do it. – Flame_Phoenix Mar 17 '14 at 11:06
  • @HarishKumar: how do I compile an example in Latex with --shell-escape in Kile? – Flame_Phoenix Mar 17 '14 at 11:08
  • OK So the issue is not really to avoid making a copy. It is to avoid making one manually? I guess since you are looking at pdfcrop or convert, the copy part is OK. – cfr Mar 17 '14 at 23:14
  • http://stackoverflow.com/questions/14098965/using-imagemagick-how-do-i-crop-out-the-white-background || http://askubuntu.com/questions/97695/is-there-a-lightweight-tool-to-crop-images-quickly – Ciro Santilli OurBigBook.com Oct 05 '15 at 22:09

1 Answers1

19

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:

MWE

\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, but as far I know this has been perfectly solved in the comments with an external tool, this is only to 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:

MWE

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

Fran
  • 80,769
  • 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
  • @Flame_Phoenix This is only a option of pdflatex: pdflatex --shell-escape file.tex compile the above MWE executing also convert. In Kile you can pass this option to the compiler in Setting > Configure Kile > Tools > Build > PDFLaTeX > General. – Fran Mar 17 '14 at 19:40
  • The dimensions: \includegraphics[trim=left bottom right top, clip] – Alex Li Oct 16 '21 at 17:01