'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, e.g. JPEG etc.
Is there a way to do this?
This link implies not:
but hints that enabling the build flag OPENCV_BUILD_3RDPARTY_LIBS might help. Searching for that flag turns up little.
NB: Please no PIL/Pillow unless it is the only way.
Thanks!
Solution 1:[1]
You can do that with pyvips like this:
#!/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
Dimensions: 3024x4032
Numpy array dimensions: (4032, 3024, 3), dtype:uint8
Solution 2:[2]
You can do that with Python Wand, which uses ImageMagick with the libheif delegate installed. Or you can simply call ImageMagick from Python using the subprocess call.
import subprocess
cmd = ['/usr/local/bin/convert','image.suffix','image.heic']
subprocess.call(cmd, shell=False)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jcupitt |
| Solution 2 | fmw42 |
