Skip to content

Instantly share code, notes, and snippets.

@dsparks
Last active June 27, 2019 00:10

Revisions

  1. dsparks revised this gist May 27, 2015. 1 changed file with 11 additions and 10 deletions.
    21 changes: 11 additions & 10 deletions kmeans_palette.R
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    doInstall <- TRUE # Change to FALSE if you don't want packages installed.
    toInstall <- c("ReadImages", "reshape", "ggplot2")
    toInstall <- c("jpeg", "reshape2", "ggplot2")
    if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
    lapply(toInstall, library, character.only = TRUE)

    @@ -29,24 +29,24 @@ allImageURLs <- c("http://media.charlesleifer.com/blog/photos/thumbnails/akira_9
    imageLoader <- function(url){ # This function takes a URL, and generates a data.frame with pixel locations and colors
    # Download to disk, load
    download.file(url, "tempPicture.jpg", mode = "wb") # Stash image locally
    readImage <- read.jpeg("tempPicture.jpg")
    readImage <- readJPEG("tempPicture.jpg")

    longImage <- melt(readImage)
    rgbImage <- reshape(longImage, timevar = "X3",
    idvar = c("X1", "X2"), direction = "wide")
    rgbImage$X1 <- -rgbImage$X1
    rgbImage <- reshape(longImage, timevar = "Var3",
    idvar = c("Var1", "Var2"), direction = "wide")
    rgbImage$Var1 <- -rgbImage$Var1
    return(rgbImage)
    }
    }

    ##########
    # Part 2 # Identifying "dominant" colors with k-means
    ##########

    rgbImage <- imageLoader(allImageURLs[5]) # Pick one, or use your own URL.
    with(rgbImage, plot(X2, X1, col = rgb(rgbImage[, 3:5]), asp = 1, pch = "."))
    rgbImage <- imageLoader("https://pbs.twimg.com/media/CGA2pXkWMAEl2Pb.jpg:large") # Pick one, or use your own URL.
    with(rgbImage, plot(Var2, Var1, col = rgb(rgbImage[, 3:5]), asp = 1, pch = "."))

    # Cluster in color space:
    kColors <- 5 # Number of palette colors
    kColors <- 6 # Number of palette colors
    kMeans <- kmeans(rgbImage[, 3:5], centers = kColors)

    zp1 <- qplot(factor(kMeans$cluster), geom = "bar",
    @@ -55,4 +55,5 @@ zp1 <- zp1 + scale_fill_manual(values = rgb(kMeans$centers))
    zp1

    approximateColor <- kMeans$centers[kMeans$cluster, ]
    with(rgbImage, plot(X2, X1, col = rgb(approximateColor), asp = 1, pch = "."))
    qplot(data = rgbImage, x = Var2, y = Var1, fill = rgb(approximateColor), geom = "tile") +
    coord_equal() + scale_fill_identity(guide = "none")
  2. dsparks created this gist Oct 30, 2012.
    58 changes: 58 additions & 0 deletions kmeans_palette.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    doInstall <- TRUE # Change to FALSE if you don't want packages installed.
    toInstall <- c("ReadImages", "reshape", "ggplot2")
    if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
    lapply(toInstall, library, character.only = TRUE)

    # Image URL:
    allImageURLs <- c("http://media.charlesleifer.com/blog/photos/thumbnails/akira_940x700.jpg",
    "http://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/402px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
    "http://upload.wikimedia.org/wikipedia/commons/thumb/e/e9/Official_portrait_of_Barack_Obama.jpg/441px-Official_portrait_of_Barack_Obama.jpg",
    "http://cache.boston.com/universal/site_graphics/blogs/bigpicture/obama_11_05/obama22_16604051.jpg",
    "http://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/758px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg",
    "http://www.10mfh.com/wp-content/uploads/2011/09/dino_riders.jpg",
    "http://images3.alphacoders.com/855/8557.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/ngm_101912/bp19.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/ngm_101912/bp26.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/ngm_101912/bp35.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/balloon/bp6.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/smithsonian_030512/bp14.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/smithsonian_030512/bp15.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/earth_day_2012/bp6.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/2011part2/bp1.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/2011part2/bp4.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/2011part2/bp15.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/2011part2/bp27.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/natural_world_2011/bp40.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/ngmphotocontest_111811/bp10.jpg",
    "http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/ngmphotocontest_111811/bp54.jpg")

    imageLoader <- function(url){ # This function takes a URL, and generates a data.frame with pixel locations and colors
    # Download to disk, load
    download.file(url, "tempPicture.jpg", mode = "wb") # Stash image locally
    readImage <- read.jpeg("tempPicture.jpg")

    longImage <- melt(readImage)
    rgbImage <- reshape(longImage, timevar = "X3",
    idvar = c("X1", "X2"), direction = "wide")
    rgbImage$X1 <- -rgbImage$X1
    return(rgbImage)
    }

    ##########
    # Part 2 # Identifying "dominant" colors with k-means
    ##########

    rgbImage <- imageLoader(allImageURLs[5]) # Pick one, or use your own URL.
    with(rgbImage, plot(X2, X1, col = rgb(rgbImage[, 3:5]), asp = 1, pch = "."))

    # Cluster in color space:
    kColors <- 5 # Number of palette colors
    kMeans <- kmeans(rgbImage[, 3:5], centers = kColors)

    zp1 <- qplot(factor(kMeans$cluster), geom = "bar",
    fill = factor(kMeans$cluster))
    zp1 <- zp1 + scale_fill_manual(values = rgb(kMeans$centers))
    zp1

    approximateColor <- kMeans$centers[kMeans$cluster, ]
    with(rgbImage, plot(X2, X1, col = rgb(approximateColor), asp = 1, pch = "."))