Skip to content

Instantly share code, notes, and snippets.

@jdbcode
Last active August 13, 2020 22:24
Show Gist options
  • Save jdbcode/2af647876e03c76de5424e15b30b74ec to your computer and use it in GitHub Desktop.
Save jdbcode/2af647876e03c76de5424e15b30b74ec to your computer and use it in GitHub Desktop.
# This script relies on the magick library, see this vignette for more information:
# https://cran.r-project.org/web/packages/magick/vignettes/intro.html
#
# If you have Windows or Mac OS, I believe ImageMagick STL is integrated with the CRAN distribution
# of the R magick library. If on Linux, see the "Build from source" section of the above URL
#
# This script will add year annotations from GIF animations generated and downloaded from the
# LT-GEE Time Series Animator open access Earth Engine App:
# https://emaprlab.users.earthengine.app/view/lt-gee-time-series-animator
#
# It will write out two files: one that goes forward in time only (*_fwd.gif) and
# one that goes forward and then backward in time (*_fwdbck.gif)
#
# 1. Copy this script into RStudio, make sure you have the magick library installed (in console: install.packages("magick"))
# 2. Edit the inputs using the example and inline comments to help
# 3. Select all lines and then press Crtl+Enter on your keyboard to run the script
# 4. The two files will be written to the same directory as the input file
# 5. Share the results widely and narrate what is happening in the video to provide context to viewers
library(magick)
####################################################################
# INPUTS
####################################################################
file = "C:/path/to/thumb.gif" # system path for GIF file downloaded from: https://emaprlab.users.earthengine.app/view/lt-gee-time-series-animator
startYear = 1984 # define the start year used in the App
endYear = 2018 # define the end year used in the App
bigger = 'yes' # would you like to make the GIF a little bigger? ('yes' or 'no')
####################################################################
yearRange = seq(startYear, endYear)
forward = image_read(file)
if(bigger == 'yes'){
scaleIt = "425"
if(image_info(forward[1])$height == 350) scaleIt = "x425"
forward = image_scale(forward, scaleIt)
}
forward = forward %>%
image_despeckle(1) %>%
image_blur(radius = 1, sigma = 0.2)
filled = c(forward[1])
comp = forward[1]
for(i in 2:length(yearRange)){
comp = image_composite(comp,forward[i])
filled = c(filled, comp)
}
for(i in 1:length(yearRange)){
filled[i] = image_annotate(filled[i], as.character(yearRange[i]), size = 30, color = "white", gravity = "north", font="Helvetica")
}
forBack = c(filled, rev(filled))
image_write(filled, path = sub('.gif', '_fwd.gif', file), format = "gif")
image_write(forBack, path = sub('.gif', '_fwdbck.gif', file), format = "gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment