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.
The vignettes for ggpattern are huge because of all the image-based examples. I want to be able to optimize and compress the vignette images from within the Rmd files to try and keep the packge under a reasonable size.
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.
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.
- PNG compression
- JPEG compression
- PDF compression
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) |
pdfopt | no | quality = ‘screen’ |
ggplot(mtcars) +
geom_density(aes(mpg, fill = as.factor(cyl))) +
theme_gray(15)
dpi <- 72
ggsave("man/figures/png-orig.png", p, width = 6, height = 4)
ggsave("man/figures/jpg-orig.jpg", p, width = 6, height = 4)
ggsave("man/figures/pdf-orig.pdf", p, width = 6, height = 4)
r1 <- pngquant (infile = "man/figures/png-orig.png", outfile = "man/figures/png-pngquant.png" , verbosity = 1)
#> pngquant: 146162 -> 50031 Space Saving: 65.8%
r2 <- pngcrush (infile = "man/figures/png-orig.png", outfile = "man/figures/png-pngcrush.png" , verbosity = 1)
#> pngcrush: 146162 -> 100636 Space Saving: 31.1%
r3 <- optipng (infile = "man/figures/png-orig.png", outfile = "man/figures/png-optipng.png" , verbosity = 1)
#> optipng: 146162 -> 98556 Space Saving: 32.6%
r4 <- zopflipng(infile = "man/figures/png-orig.png", outfile = "man/figures/png-zopflipng.png", verbosity = 1)
#> zopflipng: 146162 -> 82725 Space Saving: 43.4%
r5 <- jpegoptim(
infile = "man/figures/jpg-orig.jpg",
outfile = "man/figures/jpg-jpegoptim-lossless.jpg" ,
verbosity = 1
)
#> jpegoptim: 154167 -> 126307 Space Saving: 18.1%
r6 <- jpegoptim(
infile = "man/figures/jpg-orig.jpg",
outfile = "man/figures/jpg-jpegoptim-lossy-size.jpg",
size = 50,
verbosity = 1
)
#> jpegoptim: 154167 -> 52118 Space Saving: 66.2%
r7 <- jpegoptim(
infile = "man/figures/jpg-orig.jpg",
outfile = "man/figures/jpg-jpegoptim-lossy-quality.jpg",
quality = 10,
verbosity = 1
)
#> jpegoptim: 154167 -> 35352 Space Saving: 77.1%
r8 <- pdfopt(infile = "man/figures/pdf-orig.pdf", outfile = "man/figures/pdf-pdfopt.pdf", verbosity = 1)
#> pdfopt: 13896 -> 15731 Space Saving: -13.2%
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!