Roger
Roger

Reputation: 1853

How do I download a picture using Ruby?

I want to download this picture using Ruby. How do I do that?

http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg

I am using Mac OS.

Upvotes: 28

Views: 23535

Answers (8)

Stefan Ciprian Hotoleanu
Stefan Ciprian Hotoleanu

Reputation: 2292

Try using the Mechanize gem:

Start with: gem install mechanize to install Mechanize.

Then:

require 'rubygems'
require 'mechanize'

agent = Mechanize.new
link = 'http://www.asite.com/pic.jpg'
agent.get(link).save "images/pic.jpg" 

EDIT: To make things cleaner I would suggest you use the File's name itself when you save the image. I had an issue saving bulk images because they were not formated correctly. The images were saved like this:

#pic.jpg(1)
#pic.jpg(2)
#etc.

Thats why you should probably use the image name as the file name like this:

agent.get(src).save "images/#{File.basename(url)}"

File.basename takes the image url and just returns the the actual image name:

File.basename("http://www.asite.com/pic.jpg")
# returns the image name
pic.jpg

Upvotes: 18

Son Dang
Son Dang

Reputation: 371

There is a gem called down which is simple to use.

Firstly, install this gem by gem install down.

Then:

require "down"
file = Down.download("http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg")

You can read more on this gem in its github repo https://github.com/janko-m/down.

Upvotes: 2

Alexander Luna
Alexander Luna

Reputation: 5449

To make things cleaner I would suggest you use the File's name itself when you save the image. I had an issue saving bulk images because they were not formated correctly. The images were saved like this:

#pic.jpg(1)
#pic.jpg(2)
#etc.

Thats why you should probably use the image name as the file name like this:

src = 'http://www.asite.com/pic.jpg'    
agent.get(src).save "#{folder}/#{File.basename(src)}"

File.basename takes the image url and just returns the the actual image name:

File.basename("http://www.asite.com/pic.jpg")
# returns the image name
pic.jpg

Upvotes: 0

Fotom
Fotom

Reputation: 21

If you want to download all the images on one page, you can use the image_downloader gem:

require 'rubygems'
require 'image_downloader'

downloader = ImageDownloader::Process.new('www.test.com','img_dir/')

downloader.parse(:any_looks_like_image => true)

downloader.download()

Upvotes: 2

Geo
Geo

Reputation: 96797


require "open-uri"

open("your-url") {|f|
   File.open("whatever_file.jpg","wb") do |file|
     file.puts f.read
   end
}


Upvotes: 58

Keltia
Keltia

Reputation: 14743

The easiest way would be to require open-uri and use that with the previous answer or use the also supplied Net::HTTP module with its get method.

Upvotes: 0

Maximiliano Guzman
Maximiliano Guzman

Reputation: 2035

%x(wget http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg)

or

`wget http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg`

Upvotes: -2

Quentin
Quentin

Reputation: 943510

The same way you download anything else. Net::HTTP

Upvotes: -5

Related Questions