public
Last active

Image Manipulation, Part 1

  • Download Gist
image_manipulation_1.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
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 1 # Some image manipulation
##########
 
rgbImage <- imageLoader(allImageURLs[9]) # Pick one, or use your own URL.
 
colorColumns <- rgbImage[, substr(colnames(rgbImage), 1, 5) == "value"]
 
# Reconstitute the image in a plot()
with(rgbImage, plot(X2, X1, col = rgb(colorColumns), asp = 1, pch = "."))
 
rgbAlter <- rgbImage # Make "impressionistic"
rgbAlter$X2 <- jitter(rgbAlter$X2) # with jitter
rgbAlter$X1 <- jitter(rgbAlter$X1)
rgbAlter$Size <- runif(1:nrow(rgbAlter), 0, 2) # and random point sizes
with(rgbAlter, plot(X2, X1, col = rgb(colorColumns), asp = 1, cex = Size))
 
rgbAlter <- rgbImage
select1 <- rowSums(rgbAlter[, 1:2]^2) < 500000
rgbAlter[select1, 4] <- 0 # Zero-out green
select2 <- rgbAlter[, 1] < -500
rgbAlter[select2, 3] <- 0 # Zero-out red
with(rgbAlter, plot(X2, X1, col = rgb(rgbAlter[, 3:5]), asp = 1, pch = "."))
 
rgbAlter <- rgbImage # "Snap" colors to a smaller number of nearby colors
rgbAlter[, c(3:5)] <- round(rgbAlter[, c(3:5)] * 2) / 2
with(rgbAlter, plot(X2, X1, col = rgb(rgbAlter[, 3:5]), asp = 1, pch = "."))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.