Last active
January 16, 2023 06:36
-
-
Save SachaEpskamp/f4e3fbb4207cc95e3277d85b8a2153ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Needs https://www.jcchouinard.com/wget/ | |
pokemon_image <- function(pokemon){ | |
sapply(pokemon,function(p){ | |
tryres <- try({ | |
# URL to image: | |
url <- sprintf("https://img.pokemondb.net/artwork/large/%s.jpg",tolower(p)) | |
# set downlaod method: | |
if (grepl("apple",sessionInfo()$platform)){ | |
method <- "auto" | |
} else { | |
method <- "wget" | |
} | |
# Download image: | |
download.file(url,imgfile <- tempfile(fileext = ".jpg"), method = method,quiet = TRUE) | |
imgfile | |
}) | |
if (is(tryres,"try-error")){ | |
return(NA) | |
} else { | |
return(imgfile) | |
} | |
}) | |
} | |
pokemon_plotter <- function(pokemon){ | |
if (!require("jpeg")){ | |
install.packages("jpeg") | |
library("jpeg") | |
} | |
# Download file: | |
imgfile <- pokemon_image(pokemon) | |
# Load image: | |
img <- readJPEG(imgfile) | |
# Aspect ratio: | |
aspect <- nrow(img) / ncol(img) | |
# Plot image: | |
par(mar = rep(0,4)) | |
plot(0,type="n",bty="n",xlab="",ylab="",axes=FALSE,xlim=c(0,1),ylim=c(0,1)) | |
# Adjust aspect ratio: | |
aspect <- aspect * (par("pin")[1]/par("pin")[2]) | |
# Width and height of plot: | |
width <- 1 / max(aspect,1) | |
height <- aspect / max(aspect,1) | |
rasterImage(img,0.5-width/2,0.5-height/2,0.5+width/2,0.5+height/2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment