Skip to content

Instantly share code, notes, and snippets.

@JEFworks
Last active July 31, 2018 00:09
Show Gist options
  • Save JEFworks/3c2616a2948faf29e10f3100d3e8c1c0 to your computer and use it in GitHub Desktop.
Save JEFworks/3c2616a2948faf29e10f3100d3e8c1c0 to your computer and use it in GitHub Desktop.
Bull's eye color frequency plots
library(jpeg)
library(rvest)
## Image urls
urls <- c(
"http://www.instagram.com/p/BWIKgiuADnP/media/?size=m",
"http://www.instagram.com/p/BXY2LnqgB-g/media/?size=m",
"http://www.instagram.com/p/BZhL7zlg0cE/media/?size=m",
"http://www.instagram.com/p/BaHDmk5AOYe/media/?size=m",
"http://www.instagram.com/p/BcDKJywAMh4/media/?size=m",
"http://www.instagram.com/p/Bcw-Uc1gWP9/media/?size=m",
"http://www.instagram.com/p/BdnYTPfglOO/media/?size=m",
"http://www.instagram.com/p/BfiqdRVgCxo/media/?size=m",
"http://www.instagram.com/p/Bg8q8fJAntj/media/?size=m",
"http://www.instagram.com/p/BiF9IP5F7bf/media/?size=m",
"http://www.instagram.com/p/BiiF767Fc7O/media/?size=m",
"http://www.instagram.com/p/BkjPdUvFf8d/media/?size=m"
)
## Look at images
par(mfrow=c(4,3), mar=rep(0,4), bg='black')
imgs <- lapply(urls, function(session) {
webpage <- html_session(session)
url <- webpage$url
## Download image from url
z <- tempfile()
download.file(url, z, mode="wb")
img <- readJPEG(z)
file.remove(z) # cleanup
## Round RGB values to minimize number of total possible colors
img = round(img, digits = 1)
plot.new()
lim <- par()
rasterImage(img, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
return(img)
})
## Convert to "bulls-eye" plots inspired by http://hint.fm/projects/wired2008/
par(mfrow=c(4,3), mar=rep(1,4), bg='black')
invisible(lapply(imgs, function(img) {
## Convert from numbers to colors
colors <- sapply(1:nrow(img), function(x) {
sapply(1:ncol(img), function(y) {
rgb(img[x,y,1], img[x,y,2], img[x,y,3])
})
})
## Color frequency
common.colors <- table(colors)
## Plot common colors
#t <- quantile(common.colors, 0.5)
#t
#common.colors <- common.colors[names(which(common.colors>t))]
## Plot top 10 colors
common.colors <- sort(common.colors, decreasing=TRUE)[1:10]
library(plotrix)
radius = common.colors/max(common.colors)
plot(0,0, xlim=c(-1,1), ylim=c(-1,1), axes=FALSE, xlab=NA, ylab=NA)
lapply(1:length(radius), function(i) {
draw.circle(0, 0, radius = radius[i], col=names(radius)[i], lty=0)
})
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment