resmush is a R package that allows users to optimize and compress images using reSmush.it. reSmush.it is a free API that provides image optimization and has been implemented in WordPress and many other tools.
Some of the features of reSmush.it include:
png, jpg/jpeg, gif, bmp, tiff.Compressing an online jpg image:
library(resmush)
url <- "https://dieghernan.github.io/resmush/img/jpg_example_original.jpg"
resmush_url(url, outfile = "jpg_example_compress.jpg", overwrite = TRUE)
#> ══ resmush summary ═════════════════════════════════════════════════════════════
#> ℹ Input: 1 url with size 178.7 Kb
#> ✔ Success for 1 url: Size now is 45 Kb (was 178.7 Kb). Saved 133.7 Kb (74.82%).
#> See result in directory '.'.The compression quality for jpg files can be adjusted using the qlty argument. However, it is recommended to keep this value above 90 to maintain good image quality.
# Extreme case
resmush_url(
url,
outfile = tempfile(fileext = ".jpg"),
overwrite = TRUE,
qlty = 3
)
#> ══ resmush summary ═════════════════════════════════════════════════════════════
#> ℹ Input: 1 url with size 178.7 Kb
#> ✔ Success for 1 url: Size now is 2.2 Kb (was 178.7 Kb). Saved 176.4 Kb (98.74%).
#> See result in directory 'tempdir()'.qlty = 3), compared with Figure 1 (b).
All the functions return (invisibly) a dataset summarizing the process. The following example shows how this works when compressing a local file:
png_file <- system.file("extimg/example.png", package = "resmush")
# For the example, copy to a temporary file
tmp_png <- tempfile(fileext = ".png")
file.copy(png_file, tmp_png, overwrite = TRUE)
#> [1] TRUE
summary <- resmush_file(tmp_png, overwrite = TRUE)
tibble::as_tibble(summary[, -c(1, 2)])
#> # A tibble: 1 × 6
#> src_size dest_size compress_ratio notes src_bytes dest_bytes
#> <chr> <chr> <chr> <chr> <dbl> <dbl>
#> 1 239.9 Kb 70.7 Kb 70.54% OK 245618 72356Several other R packages also provide image optimization tools:
xfun::tinify(): Similar to resmush_file() but uses TinyPNG and requires an API key.xfun::optipng(): Compresses local files using OptiPNG, which must be installed locally.xfun::optipng() but with more options. Requires additional local software.| tool | CRAN | Additional software? | Online? | API Key? | Limits? |
|---|---|---|---|---|---|
xfun::tinify() |
Yes | No | Yes | Yes | 500 files/month (free tier) |
xfun::optipng() |
Yes | Yes | No | No | No |
| tinieR | No | No | Yes | Yes | 500 files/month (free tier) |
| optout | No | Yes | No | No | No |
| resmush | Yes | No | Yes | No | Max size 5 MB |
| tool | png | jpg | gif | bmp | tiff | webp | |
|---|---|---|---|---|---|---|---|
xfun::tinify() |
✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ |
xfun::optipng() |
✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
| tinieR | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ |
| optout | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ |
| resmush | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |