-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMake_a_4x4_gif.Rmd
162 lines (140 loc) · 5.02 KB
/
Make_a_4x4_gif.Rmd
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
---
title: "Creating a 4 x 4 gif of satellite images"
author: "EE Holmes"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = FALSE)
```
## Creating gifs of satellite images
The script downloads images from the CoastWatch ERDDAP server and then creates a gif using ImageMagick.
Load the packages.
```{r load_packages}
library(ggplot2) # plotting
library(dplyr) # for %>% pipe
library(purrr) # for map()
library(magick) # for image_* functions
library(stringr) # for the header on images
```
# get the names of each file
imgs=list()
for(j in 1:4){
year=as.character(years)[j]
fil_dir <- paste0("pngs/",tag,"_pngs_",year)
imgs[[j]] = list.files(path = fil_dir, pattern = "*.png", full.names = TRUE)
}
# Then make the gif 4x4
img = c()
imgleg = image_read("pngs/legend.png")
for(i in 1:length(imgs[[1]])){
theimgs=list()
for(j in 1:4){
theimgs[[j]]=image_read(imgs[[j]][i])
theimgs[[j]] = image_crop(theimgs[[j]], geometry_area(width = 300, height = 332, x_off = 7, y_off = 0))
}
imtop <- image_append(image_join(theimgs[[1]],theimgs[[2]]))
imbot <- image_append(image_join(theimgs[[3]],theimgs[[4]]))
# imtop <- image_append(image_join(theimgs[[1]],theimgs[[2]],theimgs[[3]],theimgs[[4]],theimgs[[5]]))
# imbot <- image_append(image_join(theimgs[[6]],theimgs[[7]],theimgs[[8]],theimgs[[9]],theimgs[[10]]))
im <- magick::image_append(image_join(imtop, imbot, imgleg), stack=TRUE)
img <- image_join(img, im)
}
imggif = image_animate(img, fps=4, loop=1)
image_write(imggif, paste0("gifs/",tag,"_",years[1],"-",years[4],".gif"))
# 1981:2017 data
# Make legend
require(png)
require(raster)
require(ggplot2)
require(grid)
img <- readPNG(fil) #read in last image
dim_img <- dim(img)
footersize <- 100 #will be different for different pngs
img_height <- dim_img[1]
img_width <- dim_img[2]
png("pngs/sst_legend.png",width=img_width, height=footersize)
grid.raster(img[(img_height-footersize):img_height, 1:img_width, ])
dev.off()
# Then make the gif
for(year in c(1987,1997,2007)){
years=year:(year+9)
# get the names of each file
imgs=list()
for(j in 1:10){
year=as.character(years)[j]
fil_dir <- paste0("pngs/",tag,"_pngs_",year)
imgs[[j]] = list.files(path = fil_dir, pattern = "*.png", full.names = TRUE)
}
img = c()
imgleg = image_read("pngs/sst_legend.png")
for(i in 1:length(imgs[[1]])){
theimgs=list()
for(j in 1:10){
theimgs[[j]]=image_read(imgs[[j]][i])
theimgs[[j]] = image_crop(theimgs[[j]], geometry_area(width = 300, height = 332, x_off = 7, y_off = 0))
}
imtop <- image_append(image_join(theimgs[[1]],theimgs[[2]],theimgs[[3]],theimgs[[4]],theimgs[[5]]))
imbot <- image_append(image_join(theimgs[[6]],theimgs[[7]],theimgs[[8]],theimgs[[9]],theimgs[[10]]))
im <- magick::image_append(image_join(imtop, imbot, imgleg), stack=TRUE)
img <- image_join(img, im)
}
imggif = image_animate(img, fps=4, loop=1)
image_write(imggif, paste0("gifs/",tag,"_",years[1],"-",years[10],".gif"))
}
# Now upload gifs to https://ezgif.com/gif-to-mp4
## More complicated example
I wanted to merge 4 images from 4 years and have a legend at the bottom. Here's how I did it using ImageMagick to make a big gif (13M) and then uploaded that to ezgif.com to make a much smaller movie.
<div style="text-align:center" markdown="1">
<video width="400" controls>
<source src="Kochin_SST_2014-17_4x4.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
## The code
Here is the code I used
```{r make_4x4_movie}
# get the names of each file
imgs=list()
for(j in 1:4){
year=as.character(2014:2017)[j]
fil_dir <- paste0("india_sst_pngs_",year)
imgs[[j]] = list.files(path = fil_dir, pattern = "*.png", full.names = T)
}
# Then make the gif
img = c()
imgleg = image_read("legend.png")
for(i in 1:length(imgs[[1]])){
theimgs=list()
for(j in 1:4){
theimgs[[j]]=image_read(imgs[[j]][i])
theimgs[[j]] = image_crop(theimgs[[j]], geometry_area(width = 230, height = 332, x_off = 28, y_off = 0))
}
imtop <- image_append(image_join(theimgs[[1]],theimgs[[2]]))
imbot <- image_append(image_join(theimgs[[3]],theimgs[[4]]))
im <- magick::image_append(image_join(imtop, imbot, imgleg), stack=TRUE)
img <- image_join(img, im)
}
imggif = image_animate(img, fps=4, loop=1)
image_write(imggif, "Kochin_SST_2014-17_4x4.gif")
```
I could also do the same with animation
```{r animation_4x4_movie}
# you can use animation package but the default settings give a low quality movie
library(animation)
library(magick)
imgleg = image_read("legend.png")
saveVideo({
for(i in 1:length(img14)){
theimgs=list()
for(j in 1:4){
theimgs[[j]]=image_read(imgs[[j]][i])
theimgs[[j]] = image_crop(theimgs[[j]], geometry_area(width = 230, height = 332, x_off = 28, y_off = 0))
}
imtop <- image_append(image_join(theimgs[[1]],theimgs[[2]]))
imbot <- image_append(image_join(theimgs[[3]],theimgs[[4]]))
im <- magick::image_append(image_join(imtop, imbot, imgleg), stack=TRUE)
plot(as.raster(im))
}
},
video.name="Kochin_SST_2014-17.mp4"
)
```