Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read image file in ruby

Tags:

ruby

how to read image file in ruby suppose i open a jpg file like this

path="c:/image/aj.jpg" File.open(path) do end

now how do i see this image file

like image 490
Milan Avatar asked Feb 08 '26 04:02

Milan


1 Answers

You can read arbitrary binary content

path = "/foo/bar/baz.jpg"
File.open(path, 'rb') {|file| file.read }

If you want to write this image to another..

File.open(path, 'rb') do |in|
  File.open("foo/bar/bob.jpg", 'wb') {|out| out.write(in.read) }
end

The binary flags are only required in Windows/DOS.

See the IO class

like image 133
Lee Jarvis Avatar answered Feb 12 '26 04:02

Lee Jarvis