Introducing 'optout' - A package for optimizing graphics output

optout - Optimized Graphics Output

This package enables access to some common command-line image optimization and compression tools from within R, with a mostly consistent call interface.

Tools are included for interfacing command line tools for compressing JPG, PNG and PDF files.

My use case - the vignettes for ggpattern are huge because of the image example, and I wanted to be able to optimize and compress images from within the vignette Rmd files.

Security Warning

This package does a lot of system2() calls with user input. I’ve sanitised all user input, but nothing is ever perfect, so in general this library should not accept input from the internet e.g. as part of a shiny app.

Installation

You can install the development version from GitHub with:

# install.packages("remotes")
remotes::install_github("coolbutuseless/optout")

This package relies on your system having installed the following command line programs. If a particular utility is not installed, the package will still load fine, but you will not be able to use that particular compression type.

Overview of available compressors

filetype compressor lossless default options
png pngquant no speed = 4, dither = FALSE
png pngcrush yes brute = FALSE
png optipng yes level = 1
png zopflipng yes lossy_alpha = FALSE, lossy_8bit = FALSE, more = FALSE, insane = FALSE
jpeg jpegoptim optional quality = NULL, size = NULL (i.e. lossless)
pdf pdfopt no quality = ‘screen’

Sample Plot

ggplot(mtcars) + 
  geom_density(aes(mpg, fill = as.factor(cyl))) + 
  theme_gray(15)

dpi <- 72
ggsave("/img/optout/png-orig.png", p, width = 6, height = 4)
ggsave("/img/optout/jpg-orig.jpg", p, width = 6, height = 4)
ggsave("/img/optout/pdf-orig.pdf", p, width = 6, height = 4)

Example: Optimizing PNG output

r1 <- pngquant (infile = "/img/optout/png-orig.png", outfile = "/img/optout/png-pngquant.png" , verbosity = 1)
r2 <- pngcrush (infile = "/img/optout/png-orig.png", outfile = "/img/optout/png-pngcrush.png" , verbosity = 1)
r3 <- optipng  (infile = "/img/optout/png-orig.png", outfile = "/img/optout/png-optipng.png"  , verbosity = 1)
r4 <- zopflipng(infile = "/img/optout/png-orig.png", outfile = "/img/optout/png-zopflipng.png", verbosity = 1)

Orig PNG

pngquant - space saving 66%

pngcrush - space saving 31%

optipng - space saving 33%

zopflipng - space saving 43%

Example: Optimizing JPEG output

r5 <- jpegoptim(
  infile    = "/img/optout/jpg-orig.jpg", 
  outfile   = "/img/optout/jpg-jpegoptim-lossless.jpg" , 
  verbosity = 1
)


r6 <- jpegoptim(
  infile    = "/img/optout/jpg-orig.jpg", 
  outfile   = "/img/optout/jpg-jpegoptim-lossy-size.jpg",
  size      = 50,  
  verbosity = 1
)


r7 <- jpegoptim(
  infile    = "/img/optout/jpg-orig.jpg", 
  outfile   = "/img/optout/jpg-jpegoptim-lossy-quality.jpg", 
  quality   = 10,
  verbosity = 1
)

Orig JPG

.

jpegoptim - lossless

Space saving 18%

jpegoptim - target size 50kB

Space saving 66%

jpegoptim - target quality 10

Space saving 77%

Example: Optimizing PDF output

r8 <- pdfopt(infile = "/img/optout/pdf-orig.pdf", outfile = "/img/optout/pdf-pdfopt.pdf", verbosity = 1)

Orig PDF

.

pdfopt

Space saving 33%

Speed

Most optimizers run in reasonable time i.e. ~1 second for a single file.

zopflipng is the exception and with higher compression options you will see the heat death of the universe before you will see it finish. Even at its lowest settings (the default) it will take tens-of-seconds up to several minutes to compress a file. Use with caution!