summary refs log tree commit diff
path: root/pnmcrop.html
blob: 83237ac41a49d21828dfc094bf738daf1a060885 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.3//EN">
<html><head><title>Pnmcrop User Manual</title></head>
<body>
<h1>pnmcrop</h1>
Updated: 16 August 2020
<br>
<a href="#index">Table Of Contents</a>

<h2>NAME</h2>
pnmcrop - crop a Netpbm image

<h2 id="synopsis">SYNOPSIS</h2>

<b>pnmcrop</b>

[<b>-white</b>
|<b>-black</b>
|<b>-sides</b>
|<b>-bg-color=</b><i>color</i>
|<b>-bg-corner=</b>{
<b>topleft</b>|<b>topright</b>|<b>bottomleft</b>|<b>bottomright</b>}
]

[<b>-left</b>]

[<b>-right</b>]

[<b>-top</b>]

[<b>-bottom</b>]

[<b>-margin=</b><i>pixels</i>]

[<b>-closeness=</b><i>closeness_percent</i>]

[<b>-borderfile=</b><i>filename</i>]

[<b>-blank-image=</b>{<b>abort</b>|<b>pass</b>|<b>minimize</b>|<b>maxcrop</b>}]

{[<b>-reportfull</b>]|[<b>-reportsize</b>]}

[<b>-verbose</b>]

[<i>pnmfile</i>]

<p>Minimum unique abbreviation of option is acceptable.  You may use
double hyphens instead of single hyphen to denote options.  You may use
white space in place of the equals sign to separate an option name
from its value.

<h2 id="description">DESCRIPTION</h2>

<p>This program is part of <a href="index.html">Netpbm</a>.

<p><b>pnmcrop</b> reads a PBM, PGM, or PPM image as input, removes
borders that are the background color, and produces the same type of
image as output.

<p>If you don't specify otherwise, <b>pnmcrop</b> assumes the
background color is whatever color the top left and right corners of
the image are and if they are different colors, something midway
between them.  You can specify that the background is white or black
with the <b>-white</b> and <b>-black</b> options or make
<b>pnmcrop</b> base its guess on all four corners instead of just two
with <b>-sides</b>.

<p>By default, <b>pnmcrop</b> chops off any stripe of background color
it finds, on all four sides.  You can tell <b>pnmcrop</b> to remove
only specific borders with the <b>-left</b>, <b>-right</b>,
  <b>-top</b>, and <b>-bottom</b> options.

<p>But note that <b>pnmcrop</b>'s determination of the background color is
independent of which edges you crop, which may not be intuitive.  For example,
imagine an image with a blue border at the top and a black border at the
bottom and you say to crop the bottom (<b>-bottom</b>).  You may have expected
to crop the black border, but you actually won't crop anything,
because <b>pnmcrop</b> considers the background color to be whatever color the
top two corners are, which is blue, and there is no blue at the bottom of the
image.  If you do want <b>pnmcrop</b> to take the background color from the
edges being cropped, use <b>-bg-corner</b>.
  
<p>If you want to leave some border, use the <b>-margin</b> option.  It
will not only spare some of the border from cropping, but will fill in
(with what <b>pnmcrop</b> considers the background color) if necessary
to get up to that size.

<p>If the input is a multi-image stream, <b>pnmcrop</b> processes each
one independently and produces a multi-image stream as output.  It chooses
where to crop independently for each image.  So if you start with a stream
of images of the same dimensions, you may end up with images of differing
dimensions.  Before Netpbm 10.37 (December 2006), <b>pnmcrop</b> ignored
all input images but the first.

<p>If you want to chop a specific amount off the side of an image, use
<b>pamcut</b>.

<p>If you want to add different borders after removing the existing ones,
use <b>pamcat</b> or <b>pamcomp</b>.


<h2 id="options">OPTIONS</h2>

<p>In addition to the options common to all programs based on libnetpbm
(most notably <b>-quiet</b>, see <a href="index.html#commonoptions">
Common Options</a>), <b>pnmcrop</b> recognizes the following
command line options:

<dl compact>
<dt><b>-white</b>

<dd>Take white to be the background color.  <b>pnmcrop</b> removes
borders which are white.

<p>You may specify at most one of <b>-black</b>, <b>-white</b>, <b>-sides</b>,
<b>-bg-color</b>, and <b>-bg-corner</b>.

<dt><b>-black</b>

<dd>Take black to be the background color.  <b>pnmcrop </b> removes
borders which are black.

<p>You may specify at most one of <b>-black</b>, <b>-white</b>, <b>-sides</b>,
<b>-bg-color</b>, and <b>-bg-corner</b>.

<dt><b>-bg-color=</b><i>color</i>

<dd>This tells <b>pnmcrop</b> what color is the background - it will crop
areas of this color.  <i>color</i> is a value that would be used as the
<a href="libnetpbm_image.html#colorname">argument of the
<b>pnm_parsecolor()</b> library routine</a>.

<p>You may specify at most one of <b>-black</b>, <b>-white</b>, <b>-sides</b>,
<b>-bg-color</b>, and <b>-bg-corner</b>.

<p>This option was new in Netpbm 10.86 (March 2019).

<dt><b>-sides</b>

<dd>Determine the background color from the colors of the four corners
of the input image.  <b>pnmcrop</b> removes borders which are of the
background color.

<p>If at least three of the four corners are the same color,
<b>pnmcrop </b> takes that as the background color.  If not,
<b>pnmcrop</b> looks for two corners of the same color in the
following order, taking the first found as the background color: top,
left, right, bottom.  If all four corners are different colors,
<b>pnmcrop</b> assumes an average of the four colors as the background
color.

<p>The <b>-sides</b> option slows <b>pnmcrop</b> down, as it reads the
entire image to determine the background color in addition to the up
to three times that it would read it without <b>-sides</b>.

<p>You may specify at most one of <b>-black</b>, <b>-white</b>, <b>-sides</b>,
<b>-bg-color</b>, and <b>-bg-corner</b>.

<dt><b>-bg-corner=</b>{<b>topleft</b>|<b>topright</b>|<b>bottomleft</b>|<b>bottomright</b>

<dd>This option indicates a corner which is background.  <b>pnmcrop</b> will
use the color of this corner as the background color and crop edges of that
color.
    
<p>You may specify at most one of <b>-black</b>, <b>-white</b>, <b>-sides</b>,
<b>-bg-color</b>, and <b>-bg-corner</b>.

<p>This option was new in Netpbm 10.86 (March 2019).

<dt><b>-left</b>

<dd>Remove any left border.

<dt><b>-right</b>

<dd>Remove any right border.

<dt><b>-top</b>

<dd>Remove any top border.

<dt><b>-bottom</b>

<dd>Remove any bottom border.

<dt><b>-margin=</b><i>pixels</i>

<dd>Leave <i>pixels</i> pixels of border.  Expand the border to this size
if necessary.

<p>This option was new in Netpbm 10.29 (August 2005).

<dt><b>-closeness=</b><i>closeness_percent</i>

<dd>Any color in the image that is at least this close to the operative
background color is considered to be background.

<p>You can use this if the image has borders that vary slightly in color, such
as would be the case in a photograph.  Consider a photograph against a white
screen.  The color of the screen varies slightly with shading and dirt and
such, but is still quite distinct in color from the subject of the
photograph.  <b>pnmcrop</b> will choose some particular shade as the
background color and if you specify an appropriate <b>-closeness</b> value, it
will correctly identify all of the screen as background and crop it off.

<p>To implement more complex rules for identifying background, use
<b>-borderfile</b>.

<p>The default is zero, which means a pixel's color must exactly match the
background color for the pixel to be considered part of a background border.

<p>This option was new in Netpbm 10.78 (March 2017).  With older Netpbm,
colors must match exactly.

<dt><b>-borderfile=</b><i>filename</i>

<dd>Use the image in the file named <i>filename</i> instead of the input
image to determine where the borders of the input image are and the
background color.

<p>Without this option, <b>pnmcrop</b> examines the input image and figures
out what part of the image is border and what part is foreground (not border),
as well as the background color.  With this option, <b>pnmcrop</b> finds the
borders in one image, then uses the those four border sizes (left, right, top,
bottom) in cropping a different image.  Furthermore, if you use
<b>-margin</b> to add borders, the color of those borders is the background
color <b>pnmcrop</b> detects in the border file.

<p>The point of this is that you may want to help <b>pnmcrop</b> to come to a
different conclusion as to where the borders are and what the background color
is by preprocessing the input image.  For example, consider an image that has
speckles of noise in its borders.  <b>pnmcrop</b> isn't smart enough to
recognize these as noise; it sees them as foreground image.  So <b>pnmcrop</b>
considers most of your borders to be foreground and does not crop them off as
you want.  To fix this, run the image through a despeckler such as
<b>pbmclean</b> and tell <b>pnmcrop</b> to use the despeckled version of the
image as the <b>-borderfile</b> image, but the original speckled version as
the input image.  That way, you crop the borders, but retain the true
foreground image, speckles and all.

<p>The border file must have the same number of images in it as the input
file; the background color determination for image N of the input is based on
the image N of the border file.

<p>This option was new in Netpbm 10.29 (August 2005).

<p>Before Netpbm 10.46 (March 2009), the original image and not the
border file determines the background color.  <b>pnmcrop</b>
fails if there is no apparent background color in the original image
(i.e. the corners of the image don't have a common color).

<dt><b>-blank-image=</b>{<b>abort</b>|<b>pass</b>|<b>minimize</b>|<b>maxcrop</b>}

<dd>This determines how <b>pnmcrop</b> handles an image which is entirely
  background (blank), a case where cropping doesn't make much sense.

  <dl>
  <dt>abort
  <dd>program fails, with explanatory message (default)

  <dt>pass
    <dd>Output image is the same as the input image.
      <b>-margin</b> has no effect.

  <dt>minimize
    <dd>output is a single row, column, or pixel (of the background color).
      If you crop both vertically and horizontally (the default), it is a
      single pixel.  If you crop only vertically, a single row, of the
      original width.  If you crop only horizontally, it is a single column,
      of the original height.

      <p>This is a somewhat incongruous result; the mathematically consistent
        result of cropping the background from an image that is entirely
        background would be an image with no pixels at all.  But such a thing
        does not exist in the Netpbm formats (and you probably wouldn't want
        it anyway, because whoever processes this output may not tolerate
        that).

      <p>The background can be more than one color when you specify
      <b>-closeness</b>, so it matters which row, column, or pixel remains.
      If you crop on the top and not bottom, it is the last row that remains.
      If you crop on both the top and bottom, it is the middle row that
      remains.  The other cases follow similarly.

      <p>If you specify a margin (<b>-margin</b>), the output image consists
        entirely of the margins; there is no single row, column, or pixel
        between the margins.  So with <b>-margin</b>, the incongruity
        mentioned above does not exist.  But before Netpbm 10.92 (September
        2020), <b>-margin</b> was ignored with <b>-blank-image=minimize</b>.

  <dt>maxcrop
    <dd>This odd function selects a hypothetical cropping which is not even
      possible, and therefore is valid only with <b>-reportfull</b> or
      <b>-reportsize</b>.  The cropping that this selects is a crop of the
      entire image on every side on which you request cropping.  So if you
      request cropping only on the left, of a 600 pixel wide image, this
      selects a cropping of 600 pixels from the left and none from the other
      three sides.  Note that were this cropping actually applied, this would
      produce an image with no pixels, which is not a valid Netpbm image.  But
      it gets stranger still if you request cropping on both the right and the
      left.  In that case, the cropping selected is a cropping of 600 pixels
      from both the right and left sides, which would leave a negative-width
      image.
      <p>
      This is actually useful if you are trying to find a single set of
      cropping parameters to crop a stream of images.  To do this, you could
      do a pass with <b>-reportsize</b> and <b>-blank-image=maxcrop</b> to
      compute the maximum crop for each edge, and then use those numbers in
      <b>-crop<i>xxx</i></b> options on a <b>pamcut</b> pass to do the crop.
      In this scenario, any all-background (blank) images would have no effect
      on the cropping parameters you compute.  If you do this, you must give
      special consideration to a stream with nothing but blank images.

  </dl>
    
<p><b>-margin</b> is always ignored when the image is all background.

<p>This option was new in Netpbm 10.86 (March 2019).

<dt><b>-reportfull</b>

<dd>
With this option, <b>pnmcrop</b> does not actually crop anything.  Instead, it
just prints to Standard Output parameters of the cropping it would have done.
The output is a single line per image, like in this example:

<pre>
  <span style="font-family: monospace;">
     0 +7 -20 -10 200 300 rgb-255:10/0/255 0.0
  </span>
</pre>

<p>The line is composed of the following blank-delimited tokens:

<ol>
  <li>how many pixels would be cropped or padded on the left.  This is
    a signed decimal number, where + means pad and - means crop.  If there
    would be no change, this is unsigned zero.

  <li>same, but for the right side.

  <li>same, but for the top.

  <li>same, but for the bottom.

  <li>the resulting image width in pixels, in decimal.

  <li>the resulting image height in pixels, in decimal.

  <li>The color <b>pnmcrop</b> took to be the background color, like
   &quot;rgb-255:10/0/255&quot; (This is a format recognized by
   the <a href="libnetpbm_image.html#colorname"><b>pnm_parsecolor()</b></a>
   library routine).  The maxval in the color specification is the maxval of
   the image.

   <li>The closeness value (see <b>-closeness</b> option) <b>pnmcrop</b>
   used, in floating point decimal.
</ol>

<p>You cannot use <b>-borderfile</b> together with this option.

<p>This option was new in Netpbm 10.86 (March 2019).

<dt><b>-reportsize</b>
<dd>
This is like <b>-reportfull</b>, but reports only the left, right, top,
bottom, width, and height.

<p>You cannot use <b>-borderfile</b> together with this option.

<p>This option was new in Netpbm 10.86 (March 2019).

<dt><b>-verbose</b>

<dd>Print on Standard Error information about the processing,
including exactly how much is being cropped off of which sides.

</dl>

<h2 id="seealso">SEE ALSO</h2>

<b><a href="pamcut.html">pamcut</a></b>,

<b><a href="pamfile.html">pamfile</a></b>,

<b><a href="pnm.html">pnm</a></b>

<h2 id="author">AUTHOR</h2>

Copyright (C) 1989 by Jef Poskanzer.


<hr>
<h2 id="index">Table Of Contents</h2>
<ul>
<li><a href="#synopsis">SYNOPSIS</a>
<li><a href="#description">DESCRIPTION</a>
<li><a href="#options">OPTIONS</a>
<li><a href="#seealso">SEE ALSO</a>
<li><a href="#author">AUTHOR</a>
</ul>
</body>
</html>