Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing image data to file in ruby using rmagick

I want to write image to a file using rmagick. Given below is my code

im = "base64encodedstring"
image = Magick::Image.from_blob(Base64.decode64(im)
image[0].format = "jpeg"
name ="something_temp"
path = "/somepath/" + name
File.open(path, "wb") { |f|
    f.write(image[0])
}

I have also tried to use f.write(image). But what get written in the file is #<Magick::Image:0x7eff0587f838>. What is the reason for this?

like image 623
nnm Avatar asked Jan 08 '14 13:01

nnm


People also ask

How to view an image in RMagick?

You will have to write the image to a file and view it with a separate image viewer. 2If you installed RMagick using Rubygemsyou must set up the RubyGems environment before using RMagick. You can do one of add the require 'rubygems'statement to your program use the -rubygems command line option

What can I draw with RMagick?

(Click the image to see the Ruby program that created it.) RMagick's primitive methods include methods for drawing points, lines, Bezier curves, shapes such as ellipses and rectangles, and text. Shapes and lines have a fill color and a stroke color.

How do I set up RubyGems with RMagick?

2If you installed RMagick using Rubygemsyou must set up the RubyGems environment before using RMagick. You can do one of add the require 'rubygems'statement to your program use the -rubygems command line option add rubygemsto the RUBYOPT environment variable See the RubyGems docfor more information.

How do I use ImageMagick's drawing commands?

ImageMagick supports a set of 2D drawing commands that are very similar to the commands and elements defined by the W3C's Scalable Vector Graphics (SVG) 1.1 Specification. In RMagick, each command (called a primitive) is implemented as a method in the Drawclass. To draw on an image, simply Create an instance of the Drawclass.


1 Answers

This should work:

image[0].write(path)
like image 91
Marek Lipka Avatar answered Nov 06 '22 20:11

Marek Lipka