Download a Collection of Images from URLs using Ruby

A quick method and loop to download a collection of images with Ruby.

So there are a set of URLs you'd like to download as images. I'll show you how you can do that with ruby. You could use this in a command line script, a rake task, or anywhere else in your code.

01: Create URL Collection

First, let's assume we have a set of URLs. It doesn't really matter where they come from (a form input, hard-coded, etc.). Let's first define these URLs as an array.

urls = [

02: Method To Download An Image From URL

Next, let's write a method that converts a URL of an image into a local image file.

It's important that wherever you place or write this method, you've included the OpenURI class.

require 'open-uri'

def download_image(url, dest)
open(url) do |u|
File.open(dest, 'wb') { |f| f.write(u.read) }

This is just a quick little method. open(url) will open the URL you've passed it. And then File.open ... and f.write ... work together to create and open a local file and then write the contents of the url to that file.

The 'wb' is the mode in which we open the local file. This StackOverflow answer pulls out the modes from the Ruby docs.

03: Loop Through The URLs

Okay, now let's create a little loop to step through the array and download each of its items.

urls.each { |url| download_image(url, url.split('/').last) }

Here we're stepping through the array of urls and then running our download_image method we just created (which means that method needs to already be defined when you run this loop).

Perhaps the only odd-looking thing is url.split('/').last. It just takes the segment of the URL after the last slash to create a local file with the same filename.

04: More Features?

That gets the trick done pretty simply, but you could take it much farther if you wanted to.

You could add some fallbacks, or rescue blocks, in case you run into an error. You could prevent the loop from crapping out just because you have one bad URL.

You could also create a directory to dump these files in, since right now they are just going to dump them in the directory in which the code is being run.

Here's a link to a gist of the code put together.

Let's Connect

Keep Reading

Command Line Scripts Using Ruby

Command line scripts aren't so bad to write when you've got Ruby on your side.

Nov 17, 2014

Dynamic Routing in Middleman

Take Middleman's dynamic pages feature, combine it with data modeling, and voila! We have dynamic routing in Middleman.

May 04, 2018

Add Search to ActsAsTaggableOn with PostgreSQL and Context

ActsAsTaggableOn is a great gem for working with tags in your application, but it can be a PITA when you want to be able to search for tags.

Jan 30, 2017