Bulk Resizing Images With R & Magick

|

I recently had to resize an awful lot of images for a client’s site in order to improve their load time. The site is very image heavy, but some of these images were over 6mb, which has serious ramifications for SEO and general user experience, especially on mobile.

In today’s post, I’m going to take you through the solution I came up with, using R to resize the images to your chosen dimensions. There weren’t really any full guides to doing this that I could find on Google, so hopefully this is helpful.

This R function will let you do the following, all in one go:

  • Bulk download a series of images
  • Load them into your R environment
  • Resize them to your specified dimensions
  • Save the resized images

It’s basically the all-in-one image resizing function for R. Handy, right?

Preparing Your Data

You’ll need to do a little bit of prep on your data in order to get this running. The easiest way to do that is to put the following in a CSV file and read them into your R environment. It doesn’t matter what you name the headers or the file.

Your CSV can contain other data, but it must contain the following as different headers:

  • The link to your images (the full URL)
  • The filename you want the new files to have (including extension)

That’s it, but you also need to know the local directory that you want to save them in.

A Polite Notice

Downloading lots of images from a site can put a strain on the server. There’s no throttling in this script, so it’ll do it as fast as your internet and your computer can do it. If you’re hitting a site with a lot of requests, you might trigger a ban or you could put a serious load on their resources, possibly even taking the site down.

Essentially, what I’m saying here is be a bit sensible with it – if you’ve got thousands of images to do, break it down into smaller chunks. Your computer will thank you, as will their server.

Lecture over, let’s look at the function.

The Bulk Image Resizer Function Using The Magick Package

Here’s the function. It requires the Magick package, which is the best image management package I’ve encountered for R.

imageResizeR <- function(x, y, z, a){

  require(magick)

  for(i in seq_along(x)){
      download.file(x[i], y[i], mode = "wb")
  }

   listOfFiles <- list.files(path = z,
                            full.names = TRUE)

   imageResizing <- for(i in seq_along(listOfFiles)){

   imFile <- image_read(listOfFiles[i])

   resized <- image_scale(imFile, a)

   image_write(resized,
               paste(listOfFiles[i]))
  }
}

Let’s break it down:

How The Function Works

Yes, I know I’m using loops instead of applys – I’ve been spending a lot of time with JavaScript lately, so this is what came naturally. Maybe I’ll tidy it up later on.

Anyway, the function utilises the Magick package and automatically installs and initialises it if it’s not currently in your environment. From there, the first loop is running down the list of images in your dataset, downloading them and saving them into the environment using your assigned filename.

From here, it creates a list of the downloaded files and tells R where we’re saving them.

Finally, the last loop runs through that list we’ve just created, reads them into R as images, resizes them to our chosen dimensions and saves each of them as an image in our selected location with the assigned name. Nice and easy.

Using The imageResizeR Function

The function works using the following parameters:

  • X: Your first variable in the CSV file, where the images are currently hosted
  • Y: The filename we want to assign to the downloaded files, also in your CSV file
  • Z: The path to which we’re going to save the files
  • A: The dimensions that we want to resize the images to. Wrap them in inverted commas and add an exclamation mark to the end

So to run it, we use the following:

imageResizeR(yourDataset$link.to.your.images, yourDataset$your.image.destination, 
    “Your Working Directory”, 
    “dimensionxdimension!”)

And there we go. All your images resized quickly and easily using R. I hope you found today’s post useful.

If you did, please sign up for my email list. I’ve got something big coming up soon, so you won’t want to miss it.