Load the Packages

library(magick)
library(magickx)

Load Example Images

# Define a Helper Function to Create URLs
image_URLs <- function(day_string, hours = 0:23) file.path(
  "http://wind.met.fu-berlin.de/loops/bwb", 
  sprintf("BWBRR_TEXT_%s_%02d00.gif", day_string, hours)
)

yesterday <- gsub("-", "", Sys.Date()-1)

# Define different sets of URLs
urls_1 <- image_URLs(day_string = yesterday, 8:10)
urls_2 <- image_URLs(day_string = yesterday, 13:23)

# Show some URLs
head(urls_1)

Read Images from URLs

With the image_read() function of the magick package you can read images from many files or URLs in one function call. This function stops if there is at least one path or URL from which no image could be read. The package magickx contains a function try_image_read() that does not stop on errors but skips files that do not exist or from which no image can be read and prints out the corresponging error messages.

Read with image_read() from the magick package

# Try to read the images
images <- image_read(urls_1)

# How many images have been read?
length(images)

The execution stops, so that the variable images is not created.

Read with try_image_read() from the magickx package

# Try to read the images
images <- try_image_read(urls_1)

# How many images have been read?
length(images)

Error messages are shown but the function completes. The variable images now contains r (n1 <- length(images)) images (r (n2 <- length(urls_1)) were requested, r (n <- n2 - n1) r ifelse(n > 1, "were", "was") skipped). For the following examples, we will download images from the second set of URLs (urls_2). This works without any error:

images <- try_image_read(urls_2)

Show Example Images

The variable images is a vector of r length(images) images. If you print them, they will be overlayed:

print(images)

Use image_animate() from the magick package to show them one after each other in a short animation:

image_animate(images, fps = 2)

Define Crop Areas

# Define path to file in the package containing crop information
file <- system.file("extdata/crop_info.csv", package = "magickx")

# Load the crop information from the file
crop_info <- read.table(file, stringsAsFactors = FALSE)

# Specify rows of interest
row_names <- c("DateTime", grep("^bwb[0:1]", rownames(crop_info), value = TRUE))

# Select the geometry strings of rows of interest and name them
geometries <- setNames(crop_info[row_names, "geometry"], row_names)

# Show the geometry strings
geometries

Create Columns from Cropped Parts of the Images

# Crop area "DateTime" in each image and append the areas vertically
image_matrix(image_crop(images, geometries["DateTime"]))

# Crop area "bwb15" in each image and append the areas horizontally
image_matrix(image_crop(images, geometries["bwb15"]), nrow = 1)

Append Image Columns Next to Each Other

# Use all geometries to crop areas, append them vertically and append the
# resulting column images horizontally
gauge_images <- lapply(geometries, image_crop, image = images)

# Number of columns in image matrix to be created
n_col <- length(gauge_images)

# Arrange vector of images in a matrix
page_image <- image_matrix(list_to_magick(gauge_images), ncol = n_col)

# Show the resulting image
page_image

Extract Rows or Columns of an Image

# Select rows and columns "positively"

#page_image[1:5, 1:10]
page_image[1:2, ]
#page_image[, c(1, 3, 5)]

# Exclude rows or columns with negative indices
#page_image[-seq(1, nrow(page_image), by = 2), -c(9:10, 18)]
#border <- c(10, 10)
#widths  <- crop_info[, "width"] + border[1]
#heights <- crop_info[, "height"] + border[2]


hsonne/magickx documentation built on Sept. 27, 2019, 6 p.m.