pyvips icon indicating copy to clipboard operation
pyvips copied to clipboard

convert numpy to avif and back

Open lchunleo opened this issue 2 years ago • 2 comments

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

lchunleo avatar Apr 14 '23 03:04 lchunleo

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 avatar Apr 14 '23 09:04 jcupitt

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

jcupitt avatar Apr 14 '23 09:04 jcupitt