Skip to content

Instantly share code, notes, and snippets.

@tylermorganwall
Created September 10, 2019 13:31
Show Gist options
  • Save tylermorganwall/3223d4e13bd198196a804e9772af18ee to your computer and use it in GitHub Desktop.
Save tylermorganwall/3223d4e13bd198196a804e9772af18ee to your computer and use it in GitHub Desktop.
Create the distracted boyfriend meme with depth of field in R
library(rayfocus)
library(doParallel)
library(foreach)
#Download images
tempfile1 = tempfile()
tempfile2 = tempfile()
download.file("https://www.tylermw.com/wp-content/uploads/2019/09/memewords.png", tempfile1)
download.file("https://www.tylermw.com/wp-content/uploads/2019/09/memedepthwords.png", tempfile2)
meme = png::readPNG(tempfile1)
memedepth = (1-png::readPNG(tempfile2)[,,1])
#Set distances
memedepth2 = memedepth
memedepth2[memedepth2 < 0.2039 + 0.001] = 0.25 #close
memedepth2[memedepth2 < 0.466666+0.001 & memedepth2 > 0.466666-0.001] = 0.40 #medium
memedepth2[memedepth2 == 0.6] = 0.75 #far
memedepth2 = memedepth2*10
#Pull focus vector
focusvec = 0.24999 + (0.4-0.25) * 1/(1 + exp(seq(10, -20, length.out = 360)/0.8)) +
(0.35) * 1/(1 + exp(seq(20, -10, length.out = 360)/0.5)) -
(0.50) * 1/(1 + exp(seq(20, -1.5, length.out = 360)/0.3))
plot(focusvec*10)
#Parallel computation with 8 cores
cl = parallel::makeCluster(8)
doParallel::registerDoParallel(cl, cores = numbercores)
foreach(i = seq(1,360,by=1), .packages = "rayfocus") %dopar% {
rayfocus::render_bokeh(meme,memedepth2,focallength = 50, filename = glue::glue("distracted{i}"),
focus = focusvec[i]*10, bokehintensity = 3)
}
@mikedillion
Copy link

- glue::glue("distracted{i}"),
+ glue::glue("diffdistracted{sprintf('%03d', i)}"),

to make filenames easier to glob over for animation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment