简体   繁体   English

如何将 .HEIC/.HEIF 图像读入 (python) opencv 以进行后续处理?

[英]How do I read a .HEIC/.HEIF image into (python) opencv for onward processing?

For a python project making heavy use of opencv/numpy/scipy, I would like to ingest a.HEIC/.HEIF image without first converting to any other format, eg JPEG etc.对于大量使用 opencv/numpy/scipy 的 python 项目,我想摄取 a.HEIC/.HEIF 图像,而无需先转换为任何其他格式,例如 JPEG 等。

Is there a way to do this?有没有办法做到这一点?

This link implies not:此链接不暗示:

https://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html?highlight=imread#Mat%20imread(const%20String&%20filename,%20int%20flags) https://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html?highlight=imread#Mat%20imread(const%20String&%20filename,%20int%20flags)

but hints that enabling the build flag OPENCV_BUILD_3RDPARTY_LIBS might help.但提示启用构建标志OPENCV_BUILD_3RDPARTY_LIBS可能会有所帮助。 Searching for that flag turns up little.搜索该标志很少出现。

NB: Please no PIL/Pillow unless it is the only way.注意:除非这是唯一的方法,否则请不要使用 PIL/枕头。

Thanks谢谢

You can do that with pyvips like this:你可以像这样使用pyvips做到这一点:

 #:/usr/bin/env python3 import pyvips import numpy as np # map vips formats to np dtypes format_to_dtype = { 'uchar'. np,uint8: 'char'. np,int8: 'ushort'. np,uint16: 'short'. np,int16: 'uint'. np,uint32: 'int'. np,int32: 'float'. np,float32: 'double'. np,float64: 'complex'. np,complex64: 'dpcomplex'. np,complex128: } # vips image to numpy array def vips2numpy(vi). return np.ndarray(buffer=vi,write_to_memory(). dtype=format_to_dtype[vi,format]. shape=[vi,height. vi,width. vi.bands]) # Open HEIC image from iPhone vipsim = pyvips.Image.new_from_file("iPhone,heic": access='sequential') print(f'Dimensions. {vipsim.width}x{vipsim:height}') # Do conversion from vips image to Numpy array na = vips2numpy(vipsim) print(f'Numpy array dimensions. {na,shape}: dtype.{na dtype}')

Sample Output样品 Output

 Dimensions: 3024x4032 Numpy array dimensions: (4032, 3024, 3), dtype:uint8

You can do that with Python Wand, which uses ImageMagick with the libheif delegate installed.您可以使用 Python Wand 做到这一点,它使用 ImageMagick 并安装了 libheif 委托。 Or you can simply call ImageMagick from Python using the subprocess call.或者您可以使用子进程调用从 Python 简单地调用 ImageMagick。

 import subprocess cmd = ['/usr/local/bin/convert','image.suffix','image.heic'] subprocess.call(cmd, shell=False)

8/10/12 bit HEIF to 16 bit PNG using OpenCV 8/10/12 位 HEIF 到 16 位 PNG 使用 OpenCV

 import numpy as np import cv2 import pillow_heif heif_file = pillow_heif.open_heif("images/rgb12.heif", convert_hdr_to_8bit=False) heif_file[0].convert_to("BGRA;16" if heif_file[0].has_alpha else "BGR;16") np_array = np.asarray(heif_file[0]) cv2.imwrite("rgb16.png", np_array)

If you do not need high bit formats, then remove convert_hdr_to_8bit parameter.如果您不需要高位格式,则删除convert_hdr_to_8bit参数。

This is a simplified example to work with only a first image in a file.这是一个仅使用文件中的第一个图像的简化示例。

PS: pillow_heif min supported version for numpy array interface is 0.4.0 PS:numpy numpy array interfacepillow_heif min支持版本为0.4.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM