Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dieghernan/resmush

Compress local and online images using the reSmush.it API service https://resmush.it/
https://github.com/dieghernan/resmush

api compress-images optimize-images r r-package resmushit

Last synced: about 2 months ago
JSON representation

Compress local and online images using the reSmush.it API service https://resmush.it/

Awesome Lists containing this project

README

        

---
output: github_document
bibliography: inst/REFERENCES.bib
link-citations: yes
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
warning = FALSE,
message = FALSE,
dev = "ragg_png",
tidy = "styler",
fig.path = "man/figures/README-",
dpi = 120,
out.width = "100%"
)
```

# resmush resmush website

[![CRAN
status](https://www.r-pkg.org/badges/version/resmush)](https://CRAN.R-project.org/package=resmush)
[![CRAN
results](https://badges.cranchecks.info/worst/resmush.svg)](https://cran.r-project.org/web/checks/check_results_resmush.html)
[![Downloads](https://cranlogs.r-pkg.org/badges/resmush)](https://CRAN.R-project.org/package=resmush)
[![R-CMD-check](https://github.com/dieghernan/resmush/actions/workflows/check-full.yaml/badge.svg)](https://github.com/dieghernan/resmush/actions/workflows/check-full.yaml)
[![R-hub](https://github.com/dieghernan/resmush/actions/workflows/rhub.yaml/badge.svg)](https://github.com/dieghernan/resmush/actions/workflows/rhub.yaml)
[![codecov](https://codecov.io/gh/dieghernan/resmush/graph/badge.svg)](https://app.codecov.io/gh/dieghernan/resmush)
[![CodeFactor](https://www.codefactor.io/repository/github/dieghernan/resmush/badge)](https://www.codefactor.io/repository/github/dieghernan/resmush)
[![r-universe](https://dieghernan.r-universe.dev/badges/resmush)](https://dieghernan.r-universe.dev/resmush)
[![DOI](https://img.shields.io/badge/DOI-10.32614/CRAN.package.resmush-blue)](https://doi.org/10.32614/CRAN.package.resmush)
[![Project Status: Active -- The project has reached a stable, usable state and
is being actively
developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![status](https://tinyverse.netlify.app/status/resmush)](https://CRAN.R-project.org/package=resmush)

**resmush** is a **R** package that allow users to optimize and compress images
using [**reSmush.it**](https://resmush.it/). reSmush.it is a [free
API]{.underline} that provides image optimization, and it has been implemented
on [WordPress](https://wordpress.org/plugins/resmushit-image-optimizer/),
[Drupal](https://www.drupal.org/project/resmushit) and [many
more](https://resmush.it/tools/).

Some of the features of **reSmush.it** are:

- Free optimization services, no API key required.
- Optimize local and online images.
- Image files supported: `png`, `jpg/jpeg`, `gif`, `bmp`, `tiff`.
- Max image size: 5 Mb.
- Compression via several algorithms:
- [**PNGQuant**](https://pngquant.org/): Strip unneeded chunks from
`png`s, preserving a full alpha transparency.
- [**JPEGOptim**](https://github.com/tjko/jpegoptim)**:** Lossless
optimization based on optimizing the Huffman tables.
- [**OptiPNG**](https://optipng.sourceforge.net/): `png` reducer that is
used by several online optimizers.

## Installation

Install **resmush** from [**CRAN**](https://CRAN.R-project.org/package=resmush)
with:

```{r, eval=FALSE}
install.packages("resmush")
```

You can install the development version of **resmush** from
[GitHub](https://github.com/) with:

```{r, eval=FALSE}
remotes::install_github("dieghernan/resmush")
```

Alternatively, you can install **resmush** using the
[r-universe](https://dieghernan.r-universe.dev/resmush):

```{r, eval=FALSE}
# Install resmush in R:
install.packages("resmush", repos = c(
"https://dieghernan.r-universe.dev",
"https://cloud.r-project.org"
))
```

## Example

Compressing an online `jpg` image:

```{r example, message=TRUE}
library(resmush)

url <- paste0(
"https://raw.githubusercontent.com/dieghernan/",
"resmush/main/img/jpg_example_original.jpg"
)

resmush_url(url, outfile = "man/figures/jpg_example_compress.jpg", overwrite = TRUE)
```

::: figure
[![](https://raw.githubusercontent.com/dieghernan/resmush/main/img/jpg_example_original.jpg){alt="Original uncompressed file"
width="100%"}](https://raw.githubusercontent.com/dieghernan/resmush/main/img/jpg_example_original.jpg)

[![](./man/figures/jpg_example_compress.jpg){alt="Optimized file"
width="100%"}](https://dieghernan.github.io/resmush/reference/figures/jpg_example_compress.jpg)

Original picture (top) 178.7 Kb and optimized picture (bottom) 45 Kb
(Compression 74.8%). Click to enlarge.


:::

The quality of the compression can be adjusted in the case of `jpg` files using
the parameter `qlty`. However, it is recommended to keep this value above 90 to
get a good image quality.

```{r jpgcompresslow, message=TRUE}
# Extreme case
resmush_url(url,
outfile = "man/figures/jpg_example_compress_low.jpg", overwrite = TRUE,
qlty = 3
)
```

::: figure
[![Low quality
figure](man/figures/jpg_example_compress_low.jpg){width="100%"}](https://dieghernan.github.io/resmush/reference/figures/jpg_example_compress_low.jpg)

Low quality image due to a high compression rate.


:::

All the functions return invisibly a data set with a summary of the process. The
next example shows how when compressing a local file.

```{r pngfile}
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)

summary <- resmush_file(tmp_png, overwrite = TRUE)

tibble::as_tibble(summary[, -c(1, 2)])
```

## Other alternatives

There are other alternatives for optimizing images with **R**:

- **xfun** [@xfun], which includes the following functions for optimizing
image files:
- `xfun::tinify()` is similar to `resmush_file()` but uses
[**TinyPNG**](https://tinypng.com/). An API key is required.
- `xfun::optipng()` compresses local files with **OptiPNG** (which needs
to be installed locally).
- [**tinieR**](https://jmablog.github.io/tinieR/) package by
[jmablog](https://jmablog.com/). An **R** package that provides a full
interface with [**TinyPNG**](https://tinypng.com/).
- [**optout**](https://github.com/coolbutuseless/optout) package by
[\@coolbutuseless](https://coolbutuseless.github.io/). Similar to
`xfun::optipng()` with more options. Requires additional software to be
installed locally.

| 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 5Mb |

: Table 1: **R** packages: Comparison of alternatives for optimizing images.

| tool | png | jpg | gif | bmp | tiff | webp | pdf |
|-------------------|-----|-----|-----|-----|------|------|-----|
| `xfun::tinify()` | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ |
| `xfun::optipng()` | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
| **tinieR** | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ |
| **optout** | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ |
| **resmush** | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |

: Table 2: **R** packages: Formats admitted.

## Citation

```{r echo=FALSE, results='asis'}
print(citation("resmush"), style = "html")
```

A BibTeX entry for LaTeX users is

```{r echo=FALSE, comment=""}
toBibtex(citation("resmush"))
```

## References