Rstats
Animation
Author

George Girton

Published

December 26, 2022

Crow Harassing Hawk

Picking up the newspapers on Christmas, I heard crows. The air was cold and I only had on reading glasses, but I could see them diving at the branch of a tall tree.

I thought I could see a hawk in the tree. I pulled out my phone camera.

I zoomed to the limit of digital zoom, and, among other photos, got this one.

A crow in sunshine

I thought I’d animate a clip, using the same technique I used last month with the hummingbird. When I stepped frame by frame while editing in the iPhone’s photos app, I got an experience of the ‘live photo’ that I wanted to share. Nothing is ever so easy, but it did turn out well enough to see the hawk flinch:

Click button at right or left to advance by frame:

Code
library(slickR)

    bird_frame_filenames <- list.files("frames/", pattern=".jpg", full.names = TRUE)

slickR(bird_frame_filenames, height = 250, width='80%') +
  settings(dots = TRUE,
  infinite= TRUE,
  speed = 0,
  fade = TRUE,
  cssEase = 'linear')
Code
## Using the slickR package, which wraps a javascript carousel library slick 

Birdland

You can see how the crow makes the hawk blink, even before the wind from his wings shakes the branch.

About five minutes later, all was quiet. Both crows and hawk had flown away. I picked up the newspapers from the driveway and went inside for breakfast.

The movie

I first tried to start with a .gif image, but for some reason the .gif export out of photos did a fade/cut between two segments of the clip, combining them in a way that did not work for this project.

So, I exported a .mov file.

Starting with the live photo ‘movie’, here is the step by step.

What I did

First, I edited the endpoints, the “In” and the “Out”.

The iPhone “live photo” stores photos in a format called ‘HEIC’. Instead of using that (my installed version of imagemagick did not seem to read it), I duplicated the ‘live photo’ to a movie, trimmed the endpoints down to just the sequence you see, then exported the .mov video file.

Here’s the code below for exporting the frames. (I ran it interactively in R Studio, it isn’t part of this Quarto page)

When I tried magick::image_read_video() in the script below, it said I needed to load or install the “av” package. No sooner said than done!

The next glitch after that was that without the “fps=NULL”, it just gives you one (1) frame. But you want thirty. Add the parameter, and length of bird2 shows 30 frames instead of one

Then, it looked like the .mov import had turned the movie 90 degrees, so it was on its side.

Fortunately, imagemagick is magic! It is the work of a line of code to turn the image:

frame = magick::image_rotate(bird2[i],90)

```{r}
library(magick)


#install.packages("av")
library(av)
library(magick)

bird2 <- magick::image_read_video("idata/crow_and_hawk.mov", fps=NULL)

#https://www.r-bloggers.com/2020/08/basic-manipulation-of-gif-frames-with-magick/

#bird2 <- magick::image_read("idata/IMG_8948.heic")

print(bird2)

length(bird2)

## Interactively look at the bird frames

bird2[1]
bird2[16]
bird2[17]
bird2[18]
bird2[19]
bird2[29]
bird2[28]

#. Ooops!  Bird is on its side!!
# for(i in 1: length(bird2)) {
#  zeropadnum <- sprintf("%03i",i)
#  eachname = paste0("crowhawk",zeropadnum,".jpg")
#  magick::image_write(bird2[i],path=eachname)
# }


for(i in 1: length(bird2)) {
  zeropadnum <- sprintf("%03i",i)
  eachname = paste0("crowhawk",zeropadnum,".jpg")
  frame = magick::image_rotate(bird2[i],90)
  magick::image_write(frame,path=paste0("frames/",eachname))
}


```

The last glitch was that the target folder “frames” must pre-exist. I thought maybe image_write would create it. You never know, you know, don’t you know? But no.

I typed in ‘mkdir frames’ at the commandline, and re-ran the script.

So, that is how I prepared the images.

— all photos Copyright © 2022-2024 George D Girton all rights reserved