Skip to content

Instantly share code, notes, and snippets.

@jasonrwang
Forked from brettkobo/photoColorAnalysis.R
Created September 6, 2016 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonrwang/98e82d10db79e1fd8ead65cf0d1f94cb to your computer and use it in GitHub Desktop.
Save jasonrwang/98e82d10db79e1fd8ead65cf0d1f94cb to your computer and use it in GitHub Desktop.
# Personal Photo Histry
library("rPlotter")
library("magrittr")
library("grid")
library("gridSVG")
library("ggplot2")
#retreives list of every .jpeg on my hard-drive
files_list1 <- list.files(path="//Volumes//Seagate Backup Plus Drive/Pictures/", pattern = ".*(jpg|JPG|jpeg|JPEG)", full.names=T, recursive=T)
files_list2 <- list.files(path="//Volumes//Seagate Backup Plus Drive/Picuters /", pattern = ".*(jpg|JPG|jpeg|JPEG)", full.names=T, recursive=T)
all_jpgs <- c(files_list1, files_list2)
#extracts the file path and creation date
info <- file.info(all_jpgs)
info_path <- info
info_path["file_path"] <- rownames(info)
#extracts the most common color from each photo. This took a while...
pic_colors <- data.frame()
for (i in 1:length(info_path$file_path)) {
pic_color <- color(info_path$file_path[i])
pic_colors <- rbind(pic_colors, t(data.frame(pic_color)))
print(i)
}
#combies the files information with the most common color
per_photo_color <- cbind(info_path, pic_colors)
write.csv(per_photo_color, file = "brett_photo_color.csv")
#----- read data in ---
per_photo_color <- read.csv("Color_Personal/brett_photo_color.csv")
b_photo_color <- per_photo_color[c("mtime", "V1")]
colnames(b_photo_color) <- c("create_date", "color")
b_photo_color <- b_photo_color[order(b_photo_color$create_date),]
#reformats the year coloum
year <- strftime(b_photo_color$create_dat, format="%Y") %>% data.frame()
b_photo_color <- cbind(b_photo_color, year)
b_photo_color["color"] <- as.character(b_photo_color$color)
b_photo_color["."] <- as.character(b_photo_color$.)
colnames(b_photo_color) <- c("create_date", "color", "year")
p_color <- subset(b_photo_color, b_photo_color$color != "NULL")
#plotting the distribution of color
#distribtion of color by year
grid.newpage()
xpos <- .05
for (p in 2006:2015) {
grid.raster(unlist(subset(p_color, year == p, select = "color")),
x = xpos,
width = .1,
height = .8,
interpolate = FALSE)
grid.text(p, x = xpos, y = .075, gp = gpar(col = "black", fontsize = 13))
xpos <- xpos + .1
}
grid.text("Personal Photo Collection", x =.5, y = .95, just = c("centre","centre"), gp = gpar(col = "black", fontsize = 30))
#exporting data visualization
grid.export("Color_Personal/colorYear.svg")
# number of photos by year
yearFreq <- table(p_color$year) %>% data.frame()
colnames(yearFreq) <- c("year", "freq")
yearFreq$year <- factor(yearFreq$year, levels = yearFreq$year[order(-as.numeric(yearFreq$year))])
yearFreqPlot <- ggplot(yearFreq, aes(x = year, y = freq)) +
geom_bar(stat = "identity", fill = "royal blue") +
coord_flip() +
labs(title = "Number of photos taken by year", y = "# of Photos", x = "Year") +
theme(legend.position="bottom", legend.direction="horizontal") +
scale_y_continuous(limits = c(0,4000), breaks = pretty(0:4000, n = 10), labels = pretty(0:4000, n = 10)) +
themeBrettrics
#crearting the svg image
svg(filename = "Color_Personal/yearFreq.svg",
width = 7,
height = 5,
pointsize = 5)
print(yearFreqPlot)
dev.off()
#---functions---
color <- function(x) {
return(tryCatch({
extract_colours(x, 1)
},
error = function(e) {
"NULL"
}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment