Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save greenpencil/5fd739f22ce3262d6d843ce45b59f7dc to your computer and use it in GitHub Desktop.
Save greenpencil/5fd739f22ce3262d6d843ce45b59f7dc to your computer and use it in GitHub Desktop.
files <- list.files("E:\\Downloads\\yarn")
pngList <- list()
for(filename in files){
tempPNG <- readJPEG(paste0("E:\\Downloads\\yarn\\",filename)) # Downloads & loads PNGs
pngList[[filename]] <- 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)
}))
# The dimensions of each item are equal to the pixel dimensions of the .PNG
flagDimensions <- t(sapply(pngList, function(ll){
dim(ll)[1:2]
}))
# Similarity, through Kruskal non-metric MDS
distance <- dist(meanRGB)
distance[distance <= 0] <- 1e-10
MDS <- isoMDS(distance)$points
plot(MDS, col = rgb(meanRGB), pch = 20, cex = 2)
# Plot:
boxParameter <- 2000 #6000 # To alter dimensions of raster image bounding box
par(bg = gray(8/9))
plot(MDS, type = "n", asp = 1)
for(ii in 1:length(pngList)){ # Go through each flag
tempName <- rownames(MDS)[ii]
Coords <- MDS[tempName, 1:2] # Get coordinates from MDS
Dims <- flagDimensions[tempName, ] # Get pixel dimensions
rasterImage(pngList[[tempName]], # Plot each flag with these boundaries:
Coords[1]-Dims[2]/boxParameter, Coords[2]-Dims[1]/boxParameter,
Coords[1]+Dims[2]/boxParameter, Coords[2]+Dims[1]/boxParameter)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment