-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initializing VIPS::Image from data #38
Comments
Hi Jonas, vips can make jpeg, png and uncompressed images from memory buffers, for example: but it's not exposed to Ruby (I think). It wouldn't be hard to add. What sort of API would you need? |
See also #27 |
👍 This and being able to put it to_blob/String would both be huge for us. Right now we do a lot of IO wrapping, making tempfiles, reading from them, etc. Not bad at our current scale but awkward and will be problematic down the road. |
I made a branch with an experimental new-from-data feature: Use like this: #!/usr/bin/ruby
require 'rubygems'
require 'vips'
include VIPS
data = IO.read('/home/john/pics/shark.jpg')
reader = JPEGReader.new(data, :shrink_factor => 2, :fail_on_warn => true)
im = reader.read_buffer
im.write('out.png') Does that look reasonable? It's passing the whole data file in the pathname argument, which is a bit odd. What type does AWS use for representing binary objects? Would RSTRING work for that as well? You have to have the whole of the compressed JPEG loaded in memory at once, but it'll only decompress the bit that it's working on, so memuse shouldn't be too crazy. |
I've fixed it up a bit and added a note to the README. Use with: jpeg_data = IO.read('/home/john/pics/wtc.jpg')
reader = JPEGReader.new(jpeg_data, :shrink_factor => 1, :fail_on_warn => true)
im = reader.read_buffer
writer = PNGWriter.new(im, :compression => 2, :interlace => false)
png_data = writer.to_memory
IO.write('out.png', png_data)
writer = JPEGWriter.new(im, :quality => 50)
jpeg_data = writer.to_memory
IO.write('out.jpg', jpeg_data) Looking at libvips again I see that PNG read from memory buffer is not actually implemented, I'll look into it. What other formats would be useful? |
Super rad! For us, gif is the third big one. Can't think of anything after that (at least until webp becomes a bigger thing). :) This will be super huge for our most common use cases (which involves pulling images down from S3 and the like). |
This is great, @jcupitt! Works for me without problems. Thanks! |
I've just added load png from memory buffer as well. It needs git master libvips. Do you have named sockets? You should be able to load some formats at least directly from a socket. GIF support is much harder to add. libvips currently uses libmagick for gif load, so you'd need to add load libmagick image from memory buffer (is this even possible? I've not checked the API) and you'd need to write save-with-libmagick and add a buffer option to that as well. Alternatively, call a gif library directly, which might be easier. vips will probably never be able to handle gif animations, it has (almost) no support for images with multiple layers. |
Awesome! Gif isn't a huge deal for us. It seldom comes up with us (and you should see what happens to animated ones when they do get cropped down - crazy mangling). I think we can handle that as an edge case (basically exactly as we're handling it right now). Thanks so much for this! |
ruby-vips 0.3.6 now has this feature included, see the README |
Closing issue as it seems done, please open a new one if you hit bugs in the new stuff. |
Hi jcupitt
Thanks for maintaining ruby-vips. We're eager to experiment with some vips power.
Is it somehow possible to instantiate a VIPS::Image from data - not from a file path?
I'm holding the data in memory after reading it from an AWS S3 Object (http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html#read-instance_method)
If not possible, how would you prefer the feature implemented?
Cheers
The text was updated successfully, but these errors were encountered: