Download, batch process, and tile images
<!DOCTYPE html>
As I continue to work on the hexsession package, which now works for arbitrary sets of images, here’s a walkthrough of the steps needed to:
- download random dog images from the Dog CEO API
- batch resize the images and make them black and white
- make a hexagonal tile of the images, each with an optional URL
I keep forgetting how to do the first two steps, so hopefully others find this helpful and I can also come back to this when I get stuck.
The final output is embedded above, which is pretty nice and this kind of thing can be used for software packages, or maybe for people in an organization with headshots and links to each member’s profile.
As usual, the code in these blocks below should be reproducible as long as you have an internet connection and the relevant packages installed.
Download dog photos
Let’s download dog photos from the Dog CEO API. This API serves images for different breeds, built from the Stanford Dogs Dataset plus community contributions. We can use httr2
to send a GET request to the API and retrieve a list of image URLs. To be nice to the servers I did all the iteration here slowly
with purrr
. First we get the list of breeds, select some at random, add two that I like, and them download the photos.
Batch process the photos
We can use magick
to resize all the images to a uniform height of 230px, desaturate them, and export each one to disk in a new folder. The trick here was to apply as.list()
to the magick image stack in the final step.
Create the tile
Now that hexsession takes arbitrary sets of image paths, we can just build a tile with a vector of paths. I added the same url to each image just to show that the html output works.
To embed the output in this post, I put the self-contained html file in _includes/ and refer to it with {% include %} .
nice!