- Published on
Command Line Tools to Convert Images to WebP Format
- Authors
- Name
- Rahul Neelakantan
WebP is a modern image format that provides superior lossless and lossy compression for images on the web. Using WebP, webmasters and web developers can create smaller, richer images that make the web faster.
Convert GIF to WebP format
gif2webp file.gif -o file.webp
cwebp
cwebp will convert different types of images, i.e., jpg/png/etc. To webp format.
> identify "example.png"
example.png PNG 1587x2245 1587x2245+0+0 8-bit sRGB 192733B 0.000u 0:00.000
This "example.png" file is of size 189 KB. after conversion to webp of the bounding box (800 x 800), the webp size will be just 13 KB. This accounts for about a 14.5 times reduction in the size of the image.
The process of converting the image to webp format is described below.
> cwebp "example.png" -o "example.webp"
Saving file 'example.webp'
File: example.png
Dimension: 1587 x 2245
Output: 41756 bytes Y-U-V-All-PSNR 49.51 50.60 48.05 49.38 dB
(0.09 bpp)
block count: intra4: 1889 (13.40%)
intra16: 12211 (86.60%)
skipped: 11620 (82.41%)
bytes used: header: 281 (0.7%)
mode-partition: 12198 (29.2%)
Residuals bytes |segment 1|segment 2|segment 3|segment 4| total
macroblocks: | 1%| 7%| 7%| 85%| 14100
quantizer: | 36 | 36 | 32 | 24 |
filter level: | 11 | 8 | 6 | 5 |
This will give you the image of the exact dimensions in webp format, which is still a 4.6 times reduction.
Resizing Image by maintaining aspect ratio
You don't want to have the full-size image and need it to be bounded to an 800 x 800 box, i.e., width and height lower than 800.
> cwebp "example.png" -resize 800 0 -o "example.webp"
Saving file 'example.webp'
File: example.png
Dimension: 800 x 1132
Output: 19598 bytes Y-U-V-All-PSNR 47.20 47.81 45.34 46.91 dB
(0.17 bpp)
block count: intra4: 898 (25.30%)
intra16: 2652 (74.70%)
skipped: 2411 (67.92%)
bytes used: header: 219 (1.1%)
mode-partition: 4401 (22.5%)
Residuals bytes |segment 1|segment 2|segment 3|segment 4| total
macroblocks: | 1%| 6%| 19%| 74%| 3550
quantizer: | 36 | 35 | 31 | 23 |
filter level: | 11 | 7 | 6 | 5 |
This will still give you an image where the height of the image is still more than 800px.
> identify "example.webp"
example.webp WEBP 800x1132 800x1132+0+0 8-bit sRGB 19598B 0.000u 0:00.010
You'll have to rerun the cwebp command by fixing height as 800, and width can be anything. This will ensure that your image is inside the bounding box of 800 x 800.
> cwebp "example.webp" -resize 0 800 -o "example.webp"
Saving file 'example.webp'
File: example.webp
Dimension: 566 x 800
Output: 12726 bytes Y-U-V-All-PSNR 45.61 46.57 44.22 45.48 dB
(0.22 bpp)
block count: intra4: 587 (32.61%)
intra16: 1213 (67.39%)
skipped: 1056 (58.67%)
bytes used: header: 177 (1.4%)
mode-partition: 2719 (21.4%)
Residuals bytes |segment 1|segment 2|segment 3|segment 4| total
macroblocks: | 2%| 7%| 21%| 69%| 1800
quantizer: | 36 | 34 | 31 | 23 |
filter level: | 11 | 7 | 6 | 5 |
> identify "example.webp"
example.webp WEBP 566x800 566x800+0+0 8-bit sRGB 12726B 0.000u 0:00.006
Convert png files in folder to webp
To convert all png files in a folder to webp format, you can use the following command.
for FILE in *; do cwebp ${FILE%%.*}.png -o ${FILE%%.*}.webp; done;
To delete all files from folder based on extension
For example, to delete all png files from the folder, you can use the following command.
rm *.png