Shrink image filesizes with the TinyPNG API.
From the TinyPNG website: “TinyPNG uses smart lossy compression techniques to reduce the file size of your files. By selectively decreasing the number of colors in the image, fewer bytes are required to store the data. The effect is nearly invisible but it makes a very large difference in file size!”
|example.png: 17 Kb||example_tiny.png: 6 Kb|
TinieR works with .png and .jpg/.jpeg files, and can return the new image filepath to enable embedding in other image workflows/functions.
You can install the latest version of tinieR from Github with:
# install.packages("devtools") devtools::install_github("jmablog/tinieR")
Once you have your API key, you can set it for your current R session with:
Be careful including your API key in any scripts you write, especially if you’re going to be publicly or privately sharing those scripts with others! You might consider setting your API key instead in your .Renviron file (~/.Renviron). If you use the variable name
TINY_API in .Renviron,
tinify() should find it, and you can skip using
tinify_api() or providing an API at each call of
To shrink an image file’s size, provide a path to the file relative to the current working directory to
tinify("example.png") #> Filesize reduced by 50%: #> example.png (20K) => example_tiny.png (10K) #> 10 Tinify API calls this month
tinify() will create a new file with the suffix ’_tiny’ in the same directory as the original file.