pyvips icon indicating copy to clipboard operation
pyvips copied to clipboard

write_to_buffer is slow as write_to_file

Open 2h4dl opened this issue 4 years ago • 7 comments

Hello, I'm using write_to_buffer to conver image to a binary object, but this is so slow.

pyvips:2.1.11 libvips:8.9.1 os: windows 10

image size: 16MB image url: https://github.com/2h4dl/image-processing-with-opencv-dlib/blob/master/0a4f3400-4b63-4d25-9354-01d472cff0ef.png wrtie_to_buffer cost time: 7.8975s wirte_to_file cost time: 7.2515s

A simple example:

pyvips.Image.write_to_buffer(".png")
pyvips.Image.write_to_file("x.png")

Is there an efficent way to get image binary data?

2h4dl avatar Mar 04 '20 10:03 2h4dl

Yes, PNG write is horribly slow. It's mostly down to libpng.

What kind of image data do you want? If you need it in PNG format, there's not much else you can do.

jcupitt avatar Mar 04 '20 10:03 jcupitt

Binary data I guess, cause need to uplaod an image over the network, not just PNG, also JPEG, GIF and other formats.

2h4dl avatar Mar 05 '20 06:03 2h4dl

That's just the speed PNG is, it's a slow format.

You could try JPG, it's a lot quicker. TIFF should be faster again, if you can use that. The fastest is raw binary data, but that's tricky to send over a network.

jcupitt avatar Mar 05 '20 09:03 jcupitt

I would like to try the raw binary data , how to get it? Trying to read C++ API, but hard to me.

2h4dl avatar Mar 05 '20 11:03 2h4dl

You need write_to_memory. There's an example here:

https://github.com/libvips/pyvips/blob/master/examples/pil-numpy-pyvips.py#L53

That makes a simple memory array of pixel values. It'll be tricky to send over a network though -- it's supposed to be for passing images between pyvips and PIL or numpy.

jcupitt avatar Mar 05 '20 12:03 jcupitt

write_to_memory gives a <_cffi_backend.buffer object at 0x0000016BFE36A1F8> , but nothing can do without read it. If I convert it to a numpy.ndarry, it will lose image attribute info, like type, format, exif and so on.

2h4dl avatar Mar 05 '20 13:03 2h4dl

Yes, exactly, it's just a raw memory array, they are difficult to work with.

If you need type, metadata etc. you need a format like JPG, PNG or TIF.

jcupitt avatar Mar 05 '20 13:03 jcupitt