Graphics, Figures & TablesHow to get image size by code (to crop it)

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
nphaibk
Posts: 23
Joined: Fri Sep 19, 2008 4:34 pm

How to get image size by code (to crop it)

Post by nphaibk »

I want to crop images (which can be done by the option 'trim = l b r t'). This means I need to know the absolute size (in points or cm or inch ...) to set the parameters for the option 'trim'.

I can look at the image for the real size, but it is not convenient because I have a lot of images. Does anyone know how to get image size by code? something like \textwidth for width of text

many thanks, Hai

Recommended reading 2024:

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

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

nphaibk
Posts: 23
Joined: Fri Sep 19, 2008 4:34 pm

How to get image size by code (to crop it)

Post by nphaibk »

I found the answer
  1. add a new length command:

    Code: Select all

    \newlength{\imagewidth}  % the name can be chosen, e.g. picwidth...
  2. Before using \imagewidth, set the width of image to it by

    Code: Select all

    \settowidth{\imagewidth}{\includegraphics{yourimage}}
  3. When using in calculation, remember to add {}, like this \imagewidth{}:

    Code: Select all

    \includegraphics[trim = 0 0 0.5\imagewidth{} 0, clip=true, width=4cm]{yourimage} % get the left part of your image
Post Reply