Giter VIP home page Giter VIP logo

Comments (6)

jcupitt avatar jcupitt commented on July 19, 2024 1

Hi again, do you mind if I close this issue? It's confusing having two open for almost the same thing. Let's continue discussion in the other one.

from pyvips.

jcupitt avatar jcupitt commented on July 19, 2024

Hello, unfortunately libvips does not support read and write from pipes. You need to write to a memory buffer, then write that to stdout.

The fastest way is as a simple C-style memory array. There's an example here of passing an image between vips, numpy and PIL:

https://github.com/jcupitt/pyvips/blob/master/examples/pil-numpy-pyvips.py

Does ffmpeg support piping uncompressed areas of memory like that? If not, I guess you'll want the image coded as a jpg. You can use write_to_buffer to make a string containing a jpg-coded image:

https://jcupitt.github.io/pyvips/vimage.html#pyvips.Image.write_to_buffer

from pyvips.

marcrleonard avatar marcrleonard commented on July 19, 2024

Thanks John, I was able to get it to work using PIL as the mechanism to write to std.in, I just had to read your jpeg buffer as bytes. See below:

image_vips = image_offset.smartcrop(self.output_raster_width, self.output_raster_height)
data = image_vips.write_to_buffer('.JPEG', Q=95)
image_bytes = PIL.Image.open(io.BytesIO(data))
image_bytes.save(p.stdin, 'JPEG')

I tested the script you provided. Fast indeed! So this begs the question (forgive me if it's a silly question), for anything Vips does at speed is it just using np under the hood? The reason I ask is, I benchmarked both Pillow and Vips doing the same operations of resize, offset, crop, and I actually found Vips to be much slower. The vips code I used is roughly as follows:

self.original_image = pyvips.Image.new_from_file(self.file_path)
self.original_image_width = self.original_image.width
self.original_image_height = self.original_image.height

for frame in range(100):
    image_resize = self.original_image.resize(zoom)
    image_offset = image_resize.copy(xoffset = int(x_total), yoffset = int(y_total))
    image_vips = image_offset.smartcrop(self.output_raster_width, self.output_raster_height)
    data = image_vips.write_to_buffer('.JPEG', Q=95)
    image_bytes = PIL.Image.open(io.BytesIO(data))
    image_bytes.save(p.stdin, 'JPEG')

Is it slow because it's reading from disk every time (It shouldn't be though, since the object is established at the beginning)? Are the cffi mappings not actually in-play when not utilizing the numpy arrays?

from pyvips.

jcupitt avatar jcupitt commented on July 19, 2024

smartcrop is very slow. Try just crop(left, top, width, height).

from pyvips.

jcupitt avatar jcupitt commented on July 19, 2024

libvips has its own pixel functions, it doesn't use numpy.

You have:

image_vips = image_offset.smartcrop(self.output_raster_width, self.output_raster_height)
data = image_vips.write_to_buffer('.JPEG', Q=95)
image_bytes = PIL.Image.open(io.BytesIO(data))
image_bytes.save(p.stdin, 'JPEG')

You're encoding jpg twice there, there's no need. Just do something like:

image_vips = image_offset.smartcrop(self.output_raster_width, self.output_raster_height)
data = image_vips.write_to_buffer('.JPEG', Q=95)
sys.stdout.write(data)

from pyvips.

marcrleonard avatar marcrleonard commented on July 19, 2024

Hey John, just played off this in the other thread :-)

from pyvips.

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.