pyvips
pyvips copied to clipboard
write_to_buffer is slow as write_to_file
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?
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.
Binary data I guess, cause need to uplaod an image over the network, not just PNG, also JPEG, GIF and other formats.
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.
I would like to try the raw binary data , how to get it? Trying to read C++ API, but hard to me.
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.
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.
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.