Skip to content

Instantly share code, notes, and snippets.

@rabdill
Last active May 27, 2020 17:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rabdill/134c042c87846fa8e1aec9ed677bc75d to your computer and use it in GitHub Desktop.
Save rabdill/134c042c87846fa8e1aec9ed677bc75d to your computer and use it in GitHub Desktop.
Turn a bitmap into an unsettling 3D map
# Inspired by this twitter thread:
# https://twitter.com/tylermorganwall/status/1088978382195437568
# Using (and borrowing code from) the immaculate documentation here:
# https://github.com/clauswilke/isoband
# https://github.com/tylermorganwall/rayshader
# Just provide a URL to the image you want to make weird; everything else should work out of the box:
image_url = "https://pbs.twimg.com/profile_images/905186381995147264/7zKAG5sY_400x400.jpg"
resolution = "200x200" # 200x200 seems manageable here; this is the factor that will increase computation time
library(magick)
library(rayshader)
matrix_from_image <- function(image) {
image_gray <- image %>% image_quantize(colorspace = "gray")
image_raster <- as.raster(image_gray)
d <- dim(image_raster)
m <- matrix(c((255-col2rgb(image_raster)[1,])), nrow = d[1], ncol = d[2], byrow = TRUE)
}
img <- image_resize(image_read(image_url), resolution)
image_mx <- matrix_from_image(img)
ambmat = ambient_shade(image_mx) # This will take a while at higher resolutions
image_mx %>%
sphere_shade(texture = "bw") %>%
add_shadow(ray_shade(image_mx,zscale=3,maxsearch = 300),0.7) %>%
add_shadow(ambmat,0.7) %>%
plot_3d(image_mx,zscale=10,fov=0,theta=135,zoom=0.75,phi=45, windowsize = c(1000,800))
@PaulLantos
Copy link

That's really cool. Do you know how to export an image from a specific view angle of the 3D plot?

@rabdill
Copy link
Author

rabdill commented May 27, 2020

@PaulLantos I'm sorry I never saw your comment! My notifications were poorly configured. In any case, I don't know how to grab a specific view, but the Rayshader documentation may have your answer.

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