Skip to content
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

Closed
JonasNielsen opened this issue Jun 14, 2013 · 12 comments
Closed

Initializing VIPS::Image from data #38

JonasNielsen opened this issue Jun 14, 2013 · 12 comments

Comments

@JonasNielsen
Copy link

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

@jcupitt
Copy link
Member

jcupitt commented Jun 14, 2013

Hi Jonas, vips can make jpeg, png and uncompressed images from memory buffers, for example:

http://www.vips.ecs.soton.ac.uk/supported/7.32/doc/html/libvips/VipsForeignSave.html#vips-jpegload-buffer

but it's not exposed to Ruby (I think). It wouldn't be hard to add. What sort of API would you need?

@jcupitt
Copy link
Member

jcupitt commented Jun 14, 2013

See also #27

@bnferguson
Copy link

👍 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.

@jcupitt
Copy link
Member

jcupitt commented Jun 15, 2013

I made a branch with an experimental new-from-data feature:

9df6029

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.

@jcupitt
Copy link
Member

jcupitt commented Jun 16, 2013

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?

@bnferguson
Copy link

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).

@JonasNielsen
Copy link
Author

This is great, @jcupitt! Works for me without problems. Thanks!
youre awesome

@jcupitt
Copy link
Member

jcupitt commented Jun 17, 2013

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.

@bnferguson
Copy link

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!

@jcupitt
Copy link
Member

jcupitt commented Jun 25, 2013

ruby-vips 0.3.6 now has this feature included, see the README

@bnferguson
Copy link

You sir, are a hero.

@jcupitt
Copy link
Member

jcupitt commented Jun 26, 2013

Closing issue as it seems done, please open a new one if you hit bugs in the new stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants