File: using-vipsthumbnail.md

package info (click to toggle)
vips 8.18.0-2
  • links: PTS
  • area: main
  • in suites: forky
  • size: 53,448 kB
  • sloc: ansic: 172,621; cpp: 12,257; python: 5,077; sh: 773; perl: 40; makefile: 25; javascript: 6
file content (225 lines) | stat: -rw-r--r-- 6,924 bytes parent folder | download | duplicates (2)
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
Title: Using > vipsthumbnail

libvips ships with a handy command-line image thumbnailer, `vipsthumbnail`.
This page introduces it, with some examples.

The thumbnailing functionality is implemented by [ctor@Image.thumbnail],
see the docs for details. You can it from any language
with a libvips binding. For example, from PHP you could write:

```php
$filename = "image.jpg";
$image = Vips\Image::thumbnail($filename, 200, ["height" => 200]);
$image->writeToFile("my-thumbnail.jpg");
```

You can also call `thumbnail_source` from the CLI, for example:

```console
$ cat k2.jpg | \
    vips thumbnail_source [descriptor=0] .jpg[Q=90] 128 | \
    cat > x.jpg
```

To thumbnail directly between a pair of pipes.

## libvips options

`vipsthumbnail` supports the usual range of `vips` command-line options. A
few of them are useful:

`--vips-cache-trace` shows each operation as libvips starts it. It can be
handy to see exactly what operations `vipsthumbnail` is running for you.

`--vips-leak` turns on the libvips memory leak checker. As well as reporting
leaks (hopefully there are none) it also tracks and reports peak memory use.

`--vips-progress` runs a progress indicator during computation. It can be
useful to see where libvips is looping and how often.

`--vips-info` shows a higher level view of the operations that `vipsthumbnail`
is running.

## Looping

`vipsthumbnail` can process many images in one command. For example:

```console
$ vipsthumbnail *.jpg
```

will make a thumbnail for every JPEG in the current directory. See the
[Path option](#path-option) section below to see how to control
where thumbnails are written.

`vipsthumbnail` will process images one after the other. You can get a good
speedup by running several `vipsthumbnail`s in parallel, depending on how
much load you want to put on your system. For example:

```console
$ parallel vipsthumbnail ::: *.jpg
```

## Thumbnail size

You can set the bounding box of the generated thumbnail with the `--size`
option. For example:

```console
$ vipsthumbnail shark.jpg --size 200x100
```

Use a single number to set a square bounding box. You can omit either number
but keep the x to mean resize just based on that axis, for example:

```console
$ vipsthumbnail shark.jpg --size 200x
```

Will resize to 200 pixels across, no matter what the height of the input image
is.

You can append `<` or `>` to mean only resize if the image is smaller or larger
than the target.

You can append `!` to force a resize to the exact target size, breaking
the aspect ratio.

## Cropping

`vipsthumbnail` normally shrinks images to fit within the box set by `--size`.
You can use the `--smartcrop` option to crop to fill the box instead. Excess
pixels are trimmed away using the strategy you set. For example:

```console
$ vipsthumbnail owl.jpg --smartcrop attention -s 128
```

Where `owl.jpg` is an off-centre composition:

![Owl](owl.jpg)

Gives this result:

![Smartcrop](tn_owl.jpg)

First it shrinks the image to get the vertical axis to 128 pixels, then crops
down to 128 pixels across using the `attention` strategy. This one searches
the image for features which might catch a human eye, see
[method@Image.smartcrop] for details.

## Linear light

Shrinking images involves combining many pixels into one. Arithmetic
averaging really ought to be in terms of the number of photons, but for
historical reasons the values stored in image files are usually related
to the voltage that should be applied to the electron gun in a CRT display.

`vipsthumbnail` has an option to perform image shrinking in linear space, that
is, a colourspace where values are proportional to photon numbers. For example:

```console
$ vipsthumbnail fred.jpg --linear
```

The downside is that in linear mode none of the very fast shrink-on-load
tricks that `vipsthumbnail` normally uses are possible, since the shrinking is
done at encode time, not decode time, and is done in terms of CRT voltage, not
photons. This can make linear light thumbnailing of large images slow.

For example, for a 10,000 x 10,000 pixel JPEG I see:

```console
$ time vipsthumbnail wtc.jpg
real	0m0.317s
user	0m0.292s
sys	0m0.016s
$ time vipsthumbnail wtc.jpg --linear
real	0m4.660s
user	0m4.640s
sys	0m0.016s
```

## Path option

Use `--path` to control where and how the thumbnail is written.

Three substitutions are performed on the argument: `%s` is  replaced by
the input basename with any suffix removed, `%d` is replaced by the input
dirname, and `%c` is replaced by the current working directory.

The default value is  `%d/tn_%s.jpg` meaning JPEG output, to the same
directory as the input file, with `tn_` prepended. You can add format options
too, for example `%c/%s/tn_%s.jpg[Q=20]` will  write JPEG  images to a tree
within the current directory with `Q` set to 20, or `tn_%s.png` will write
thumbnails as PNG images.

The `keep` option to savers is especially useful. Many image have very
large IPTC, ICC or XMP metadata items embedded in them, and removing these
can give a large saving.

For example:

```console
$ vipsthumbnail 42-32157534.jpg
$ ls -l tn_42-32157534.jpg
-rw-r–r– 1 john john 6682 Nov 12 21:27 tn_42-32157534.jpg
```

`keep=none` almost halves the size of the thumbnail:

```console
$ vipsthumbnail 42-32157534.jpg -path x.jpg[optimize_coding,keep=none]
$ ls -l x.jpg
-rw-r–r– 1 john john 3600 Nov 12 21:27 x.jpg
```

## Colour management

`vipsthumbnail` will optionally put images through LittleCMS for you. You can
use this to move all thumbnails to the same colour space. All web browsers
assume that images without an ICC profile are in sRGB colourspace, so if
you move your thumbnails to sRGB, you can strip all the embedded profiles.
This can save several kb per thumbnail.

For example:

```console
$ vipsthumbnail shark.jpg
$ ls -l tn_shark.jpg
-rw-r–r– 1 john john 7295 Nov  9 14:33 tn_shark.jpg
```

Now transform to sRGB and don't attach a profile (you can also use
`keep=none`, though that will remove *all* metadata from the image):

```console
$ vipsthumbnail shark.jpg --output-profile srgb --path tn_shark.jpg[profile=none]
$ ls -l tn_shark.jpg
-rw-r–r– 1 john john 4229 Nov  9 14:33 tn_shark.jpg
```

(You can use the filename of any RGB profile. The magic string `srgb` selects a
high-quality sRGB profile that's built into libvips.)

`tn_shark.jpg` will look identical to a user, but it's almost half the size.

You can also specify a fallback input profile to use if the image has no
embedded one. For example, perhaps you somehow know that a JPEG is in Adobe98
space, even though it has no embedded profile.


```console
$ vipsthumbnail kgdev.jpg --input-profile /my/profiles/a98.icm
```

## Final suggestion

Putting all this together, I suggest this as a sensible set of options:

```console
$ vipsthumbnail fred.jpg \
    --size 128 \
    --output-profile srgb \
    --path tn_%s.jpg[optimize_coding,keep=none]
```