Background
I am enjoying the magick package at the moment. Reading through the vignette I spotted the image_morph() function. In this post I experiment with the function to build the GIF below that shows the changes in the England football first kit over time, using images from the excellent Historical Football Kits website.
Scraping
The Historical Football Kits website has a detailed section on England kits spread over six pages, starting from the first outfits used in 1872. Each pages includes some interesting discussion - and importantly for this post - images of the kits.
We can use the read_html() from the xml2 package and map() from purrr to read and save the source code of each page.
library(rvest)
library(tidyverse)
htmls %
map(read_html)
From the source code we can then find the URLs of each kit image files using html_nodes() and html_attr() from rvest. I used purrr’s map_dfr() to store the links in a tibble and then dropped rows that do not contain kit image links or are images of away kits, kits used in single game or links to shops to buy replicas. This filtering was based on the image label or image URL and performed with the aid of the str_detect() function from stringr.
library(stringr)
scrape_img_url %
html_nodes(&quot;.float p , .float img&quot;) %__%
html_attr(&quot;src&quot;) %__%
tbl_df() %__%
set_names(&quot;img_url&quot;) %__%
mutate(label = html %__% html_nodes(&quot;.float p , .float img&quot;) %__%
html_text() %__%
c(., NA) %__%
.[-1])
}
d1 % map_dfr(scrape_img_url) %__%
filter(str_detect(string = img_url, pattern = &quot;/international/england&quot;),
!str_detect(string = label, pattern = &quot;change|alternate|Alternate|Change&quot;),
!str_detect(string = label, pattern = &quot; v |Third&quot;),
!str_detect(string = img_url, pattern = &quot;lithuania|italy|yellow|red&quot;))
head(d1)
## img_url label
## 1 /international/england/images/england-1872.gif 1872
## 2 /international/england/images/england-1882.gif 1879-1900
## 3 /international/england/images/england-1900.gif 1900-1914
## 4 /international/england/images/england-1920-1932.gif 1920-1930
## 5 /international/england/images/england-1921.gif 1930-1934
## 6 /international/england/images/england-1934.gif 1934
Given these URLs I then downloaded each of the images which are stored in a single R object kits
library(magick)
kits %
mutate(img_url = paste0(&quot;http://www.historicalkits.co.uk&quot;, img_url),
img_url = str_replace(string =img_url, pattern =&quot; &quot;, replacement = &quot;%20&quot;)) %__%
select(img_url) %__%
map(image_read) %__%
set_names(&quot;img&quot;)
Typing kits into R will display each kit in the RStudio viewer (it will quickly run through each image). The console displays summary information for each image in the kits object.
__ kits
$img
format width height colorspace filesize
1 GIF 170 338 sRGB 0
2 GIF 170 338 sRGB 0
3 GIF 170 338 sRGB 0
4 GIF 170 338 sRGB 0
5 GIF 170 338 sRGB 0
6 GIF 170 338 sRGB 0
7 GIF 170 338 sRGB 0
8 GIF 170 338 sRGB 0
9 GIF 170 338 sRGB 0
10 GIF 170 338 sRGB 0
Annotating Images
Before creating any GIF I wanted add annotations for the year and the copyright information. To do this I first created a border using image_border() in magick and then image_annotate() to add the text. I wrapped these edits into an add_text() function and then applied each to the kit images.
add_text %
image_border(geometry = &quot;10x60&quot;, color = &quot;white&quot;) %__%
image_chop(&quot;0x45&quot;) %__%
image_annotate(text = label, gravity = &quot;north&quot;) %__%
image_annotate(
text = &quot;Animation by @guyabelguyabel&quot;, gravity = &quot;south&quot;, location = &quot;+0+45&quot;
) %__%
image_annotate(
text = &quot;Images are Copyright of Historical\nFootball Kits and reproduced by\nkind permission.&quot;,
gravity = &quot;south&quot;
)
}
for(i in 1:length(kits$img)){
kits$img[i]