-
Notifications
You must be signed in to change notification settings - Fork 0
/
animate.R
71 lines (69 loc) · 2.09 KB
/
animate.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#' Animate a set of pngs
#'
#' @param dir
#' @param gif
#' @param regexp
#' @param glob
#' @param delay
#' @param loop
#' @param max_size
#' @param repeat_last
#' @export
animate <- function(dir, gif = "animation.gif", regexp = NULL, glob = NULL,
delay = 2, loop = TRUE,
max_size = 1200, repeat_last = 2) {
image_files <- fs::dir_ls(dir, glob = glob, regexp = regexp) |> sort()
rui::approve("Found {length(image_files)} {.path .{fs::path_ext(image_files[1])}} images")
if (fs::path_ext(image_files[1]) == "jpg") {
rui::inform("Note that {.code gifski::gifski()} only works with {.path .png} files")
rui::inform("I will attempt converting your images now")
rui::begin("Converting {.path .jpg} to {.path .png}")
convert_jpg_to_png(image_files)
rui::succeed()
png_files <- image_files %>% fs::path_ext_set("png")
} else {
png_files <- image_files
}
rui::begin("Checking dimensions")
info <- magick::image_read(png_files[length(png_files)]) %>%
magick::image_info()
original_max_size <- max(info$width, info$height)
if (original_max_size > max_size) {
info$width <- round(info$width / original_max_size * max_size)
info$height <- round(info$height / original_max_size * max_size)
}
rui::succeed()
if (repeat_last != 1) {
png_files <- c(png_files, rep(png_files[length(png_files)], repeat_last))
}
rui::begin("Combining frames")
gifski::gifski(
png_files,
gif_file = fs::path(dir, gif),
width = info$width,
height = info$height,
delay = delay,
loop = loop,
progress = FALSE
)
rui::succeed()
if (fs::path_ext(image_files[1]) == "jpg") {
rui::begin("Removing {.path .png} files")
fs::file_delete(unique(png_files))
rui::succeed()
}
rui::inform("You can find the animation at:")
rui::inform("{.path {fs::path(dir, gif)}}")
invisible()
}
convert_jpg_to_png <- function(paths) {
new_paths <- paths %>%
fs::path_ext_set("png")
purrr::walk2(
paths,
new_paths,
~ magick::image_read(.x) %>%
magick::image_convert("png") %>%
magick::image_write(.y)
)
}