libvips / pyvips

python binding for libvips using cffi
MIT License
648 stars 49 forks source link

convert numpy to avif and back #399

Open lchunleo opened 1 year ago

lchunleo commented 1 year ago

Hi

i wish to know is it possible to 1) Convert numpy to Avif format 2) Convert Avif back to numpy

Would appreciate if there is some examples how to do so in python. Thanks

jcupitt commented 1 year ago

Hi @lchunleo,

Sure, eg.:

$ python3
Python 3.10.7 (main, Mar 10 2023, 10:47:39) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyvips
>>> x = pyvips.Image.new_from_file("x.avif")
>>> numpy_array = x.numpy()

Then to save again:

>>> y = pyvips.Image.new_from_array(numpy_array)
>>> y.write_to_file("y.avif")
>>> 

There are some options to control the process, see the docs.

https://www.libvips.org/API/current/VipsForeignSave.html#vips-heifload

https://www.libvips.org/API/current/VipsForeignSave.html#vips-heifsave

jcupitt commented 1 year ago

Also, setting access="sequential" on new_from_file() can help lower memory use if all you're doing is converting to numpy.