Skip to content

Instantly share code, notes, and snippets.

@dsparks
Created November 28, 2012 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dsparks/4161836 to your computer and use it in GitHub Desktop.
Save dsparks/4161836 to your computer and use it in GitHub Desktop.
Raster mosaic images
# Drawing a mosaic composite image
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("png", "devtools", "RCurl", "ReadImages", "reshape")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Some helper functions, lineFinder and makeTable
source_gist("818983")
source_gist("818986")
# In as few lines as possible, get URLs for .PNGs of each flag
importHTML <- readLines("http://en.wikipedia.org/wiki/World_flags")
importHTML[lineFinder("thumbborder", importHTML)]
pngURLs <- makeTable(makeTable(importHTML[lineFinder("thumbborder",
importHTML)], "src=\"//")[, 2], "\" width=\"")[, 1]
pngURLs <- paste0("http://", pngURLs)
# CAUTION: The following script will literally download and save 204 .PNG
# images of flags to your working directory. Use at your own risk,
# and you might want to setwd().
doDownload <- TRUE # Set to FALSE if you've already downloaded the PNGs.
pngList <- list()
for(ii in 1:length(pngURLs)){
tempName <- paste("Pic", ii)
if(doDownload){
download.file(pngURLs[ii], # This reads PNGs directly from the
paste0(tempName, ".png"), # URL, and stashes them in the
mode = "wb") # working directory
}
tempPNG <- readPNG(paste0(tempName, ".png")) # And this re-loads them
pngList[[tempName]] <- tempPNG # And assigns them to a list.
}
# Very simple dimension reduction -- just the mean R, G, and B values
meanRGB <- t(sapply(pngList, function(ll){
apply(ll[, , -4], 3, mean)[1:3]
}))
# Load an image to composite
url <- "http://photos3.pix.ie/0F/D7/0FD7466E51E44BA283B6A95BFD9166A1-0000342864-0002483378-00064S-00000000000000000000000000000000.jpg"
url <- "http://api.ning.com/files/k0*RRphj2a3d4KgfBmYqmm0uZkUyAxRApszj8AIAeviJ4dW*VdReJymuGbp6f1vZG3tIlIYhG1Kx*DJqx9hnVAN2hmLspugL/1250834834817265300x291Cute_Puppy_by_awibawa.jpg?crop=1%3A1&width=64"
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[, 1:2] <- sweep(rgbImage[, 2:1], 2, c(100, -100), "*")
plot(rgbImage[, 1:2], col = rgb(rgbImage[, 3:5]), pch = 19, asp = 1)
# Plot:
par(mai = c(0, 0, 0, 0))
plot(rgbImage[, 1:2], type = "n", asp = 1)
for(ii in 1:nrow(rgbImage)){ # Go through pixel in the large image
targetRGB <- rgbImage[ii, 3:5] # v Finds the most-similar tile image
nearestPic <- which.min(rowSums(sweep(meanRGB, 2, unlist(targetRGB))^2))
Coords <- rgbImage[ii, 1:2]
rasterImage(pngList[[nearestPic]], # Plot each pic with these boundaries:
Coords[1]-50, Coords[2]-50,
Coords[1]+50, Coords[2]+50)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment