Skip to content

Instantly share code, notes, and snippets.

@rabdill
Last active May 27, 2020 17:46
Show Gist options
  • Save rabdill/134c042c87846fa8e1aec9ed677bc75d to your computer and use it in GitHub Desktop.
Save rabdill/134c042c87846fa8e1aec9ed677bc75d to your computer and use it in GitHub Desktop.

Revisions

  1. rabdill revised this gist Jan 26, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion raytrace_photos.r
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,6 @@
    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(isoband)
    library(magick)
    library(rayshader)

  2. rabdill created this gist Jan 26, 2019.
    32 changes: 32 additions & 0 deletions raytrace_photos.r
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    # 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(isoband)
    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))