Code Monkey home page Code Monkey logo

Comments (7)

jcupitt avatar jcupitt commented on May 17, 2024

Hi, you need the function vips_jpegload_buffer(), unfortunately not available in Python. Hopefully this is something that will be improved when we move to the new Python binding.

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

The two functions you found are for wrapping memory buffers of uncompressed pixels up as VIPs images. You can use them to move images between PIL and VIPS efficiently, see:

http://www.vips.ecs.soton.ac.uk/index.php?title=Python_Examples

So an option would be to use the PIL loader, then wrap the decompressed image as VIPS.

from libvips.

stepchowfun avatar stepchowfun commented on May 17, 2024

I thought that might be the case. I only want to do an affine transform on an image in memory. If I need to open the image with PIL, would you recommend just doing the transform in PIL as well?

Thanks for your quick responses.

from libvips.

jcupitt avatar jcupitt commented on May 17, 2024

I think the pros and cons would be:

VIPS would be faster and would be able to produce just parts of the rotated
image on demand, which might be handy. It has much better interpolators
than PIL and can generate pixels in the background, but sadly not from the
current Python binding.

PIL would be simpler to code and simpler to deploy. And have better
documentation!

On Monday, 30 July 2012, Stephan Boyer wrote:

I thought that might be the case. I only want to do an affine transform
on an image in memory. If I need to open the image with PIL, would you
recommend just doing the transform in PIL as well?

Thanks for your quick responses.


Reply to this email directly or view it on GitHub:
https://github.com/jcupitt/libvips/issues/23#issuecomment-7362999

from libvips.

scyclops avatar scyclops commented on May 17, 2024

Any update on if/when the Python bindings will support jpegload_buffer (as well as gifload_buffer and pngload_buffer)?

from libvips.

jcupitt avatar jcupitt commented on May 17, 2024

Hi @scyclops, yes, the new Python binding was released with version 8 in 2015.

http://jcupitt.github.io/libvips/API/current/using-from-python.html

Use it like this:

$ python
Python 2.7.13 (default, Jan 19 2017, 14:48:08) 
[GCC 6.3.0 20170118] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> gi.require_version('Vips', '8.0')
>>> from gi.repository import Vips
>>> with open("pics/k2.jpg", 'rb') as f:
...     str = f.read()
... 
>>> image = Vips.Image.new_from_buffer(str, "")
>>> image.avg()
102.80430080369972

The extra "" argument is for any load options you might want to set as strings.

There it's sniffing the file type from the first few bytes of the string and calling the appropriate loader for you. You can also call the jpeg loader directly:

>>> image = Vips.Image.jpegload_buffer(str)
>>> image.width
1450
>>> image.avg()
102.80430080369972

You can give optional arguments, for example:

>>> image = Vips.Image.jpegload_buffer(str, shrink = 2, access = Vips.Access.SEQUENTIAL)
>>> image.width
725
>>> image.avg()
102.80430080369972

Now it's loading in sequential mode, and at half size. You can give extra args to .new_from_buffer() as well.

There are quite a few more buffer loaders now:

$ vips -l | grep load_buffer
          VipsForeignLoadPdfBuffer (pdfload_buffer), load PDF with libpoppler, priority=0, is_a_buffer, get_flags, get_flags_filename, header, load
          VipsForeignLoadSvgBuffer (svgload_buffer), load SVG with rsvg, priority=0, is_a_buffer, get_flags, get_flags_filename, header, load
          VipsForeignLoadGifBuffer (gifload_buffer), load GIF with giflib, priority=0, is_a_buffer, get_flags, get_flags_filename, header
        VipsForeignLoadPngBuffer (pngload_buffer), load png from buffer, priority=0, is_a_buffer, get_flags, header, load
          VipsForeignLoadJpegBuffer (jpegload_buffer), load jpeg from buffer, priority=0, is_a_buffer, get_flags, header, load
          VipsForeignLoadWebpBuffer (webpload_buffer), load webp from buffer, priority=-50, is_a_buffer, get_flags, header, load
          VipsForeignLoadTiffBuffer (tiffload_buffer), load tiff from buffer, priority=0, is_a_buffer, get_flags, header, load
          VipsForeignLoadMagickBuffer (magickload_buffer), load buffer with ImageMagick, priority=-100, is_a_buffer, get_flags, get_flags_filename, header

from libvips.

scyclops avatar scyclops commented on May 17, 2024

Great - it looks like I may have been using the old python binding that I installed by running the below on Ubuntu 16.04.

apt-get install python-vipscc

Any clue on how to easily install the new python bindings on Ubuntu 16.04? It looks like running the following on Ubuntu 16.04 installs vips 8.2.2 with the old bindings but the typelib file is nowhere to be found after the install and from gi.repository import Vips always fails. Or is compiling from source the the only way to get the typelib installed?

apt-get install libvips libvips-tools python-vipscc gobject-introspection python-gi --no-install-recommends

from libvips.

jcupitt avatar jcupitt commented on May 17, 2024

Hi, python-vipscc is the old binding, you don't need to install it, unless you really want to use it.

The typelib is in gir1.2-vips-8.0. It used to not include the overrides file, unfortunately. I filed a bug, but I don't know if it was fixed for 16.04.

If you are missing the Vips.py overrides, you can download here:

https://github.com/jcupitt/libvips/blob/master/python/packages/gi/overrides/Vips.py

There's a note in the comment at the top saying where to copy the file to.

from libvips.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.