rmagick is there a way to convert image in memory
# assuming you have an image
# img = Magick::Image.new( 100, 100 )
img = Magick::Image.from_blob( img.to_blob { self.format = "png" } )
Source: RMagick Docs
Here's an example on how to give it to the user
image.format = "png"
send_data image.to_blob,
:filename => "woohoo.png",
:disposition => 'inline',
:quality => 90,
:type => 'image/png'
Converting SVG to PNG with RMagick in memory
The RMagick doc for from_blob says that from_blob returns an array of images, so my guess is that this should work:
arr = Magick::Image.from_blob(str)
str = arr[0].to_blob
RMagick - convert file to another format without saving to disk
You can specify the format when calling to_blob
. From the fine manual:
to_blob
img.to_blob
[ { optional arguments } ]-> string[...]
No required arguments, however you can specify the image format (such as JPEG, PNG, etc.) and depth by calling the format and depth attributes, as well as otherImage::Info
attributes as appropriate, in a block associated with the method.
So you can say things like this:
png_bytes = img.to_blob { |attrs| attrs.format = 'PNG' }
Yes, the interface to to_blob
is a bit odd but the strange interface is just part of the fun of working with ImageMagick.
You can also use the format=
method before calling to_blob
:
img.format = 'PNG'
png_bytes = img.to_blob
Generate animated rotating image and displaying in memory version using rmagick and Sinatra
I found a solution - not sure if it's optimal at all but it works. I hope someone out there has a better answer than me :)
I forced the img
I passed into self.spin
to be a gif, and that made it so the image animated. Here's what my self.spin
method looks like now:
def self.spin(img)
spinny_image = Magick::ImageList.new
img.format = "gif"
spinny_image << img
spinny_image << img.rotate(90)
spinny_image << img.rotate(180)
spinny_image << img.rotate(270)
spinny_image
end
RMagick - Correct way to create a thumbnail without leaking memory
Try minimagic, which was written to deal with exactly the problem you're having.
For most tasks, the minimagic gem linked here is sufficient. It runs ImageMagick's command-line tools, which means a new process is created, it runs, and then stops, freeing memory. We replaced RMagic with MiniMagic and our memory woes were a thing of the past.
Rails rmagick - Converted Image Turns Solid Black When 'resize_to_fit' applied
It's possible your PDF file has a transparent background, which is causing the problem. Try removing the alpha channel before the resize using
pdf.first.alpha(Magick::DeactivateAlphaChannel)
pdf.first.resize_to_fit!("600")
RMagick with a direct form submitted Image from Sinatra
You need to get the path from the tempfile and pass that to Magick::Image’s read
.
Here’s an example:
post "/upload-photo" do
image = Magick::Image.read(params[:image][:tempfile].path)[0]
image.crop_resized! 100, 100, Magick::CenterGravity
store_image_data image.to_blob
redirect "/done"
end
Use convert method of imagemagick in ruby on rails application
This guy looks like answer your question: https://stackoverflow.com/a/17426622/159995.
Or you can just use system('convert /home/icicle/Desktop/images_284.tif /home/icicle/Desktop/images_284.jpg')
Related Topics
Ruby Facebook Graph API Appsecret_Proof
Splitting a String into Words and Punctuation with Ruby
Ruby: Why Are All Coordinates Getting Updated in an Array of Arrays
Ruby 2.1 with Erubis Template Engine
Rails 4 - Heroku SQLite3 Error
I Trying to Make a Code That Gives the User a Personal Number After They Have Made an User
Accessing a Ruby Hash with a Variable as the Key
Expressing Conditional Haml Possibly with Ternary Operator
Ruby: Wait for All Threads Completed Using Join and Threadswait.All_Waits - What the Difference
How to Combination/Permutation in Ruby
Jekyll:New Posts Not Being Generated
How to Collapse Double Splat Arguments into Nothing
Ruby's "Foo = True If !Defined? Foo" Won't Work as Expected
In Ruby How to Create a Local Variable Explicitly
Symbol#To_Proc Shorthand with the Stabby Lambda Syntax