nish-kishore / sirfunctions

Key functions used by the SIR team
Other
0 stars 0 forks source link

Automatically compress images when writing out plots #65

Closed nish-kishore closed 2 months ago

nish-kishore commented 7 months ago

When writing images (PNGs, JPEGs, etc) out to final plots use the optoutput package (https://github.com/coolbutuseless/optout) to write a full high res file to temp and then compress it as far as we can before writing it out to its final destination. Could be written into function which wraps around ggsave.

mcuadera commented 4 months ago

tried using this function but ran into this error:

Error in system2("which", prog_name, stdout = TRUE, stderr = NULL) : 
  '"which"' not found

I am instead using pngquant, which does a nice job with compression this way instead:

fig_path <- file.path(here(), country, dr_year, "figures")
png_files <- list.files(fig_path, pattern = "*.png", full.names = TRUE)
pngquant_path <- file.path("C:", "pngquant", "pngquant.exe")
lapply(png_files, function(file) system2(pngquant_path, args = c("--ext", "_compressed.png", file)))

The code above doesn't overwrite the original photo, but is possible to do so. This requires installation of pngquant somewhere.