【发布时间】:2019-06-21 02:04:16
【问题描述】:
High Efficiency Image File (HEIF) 格式是从 iPhone 向 OSX 设备空投图像时的默认格式。我想用 Python 编辑和修改这些 .HEIC 文件。
我可以修改手机设置以默认保存为 JPG,但这并不能真正解决能够使用其他文件类型的问题。我仍然希望能够处理 HEIC 文件以进行文件转换、提取元数据等 (Example Use Case -- Geocoding)
枕头
这是尝试读取此类文件时使用 Python 3.7 和 Pillow 的结果。
$ ipython
Python 3.7.0 (default, Oct 2 2018, 09:20:07)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from PIL import Image
In [2]: img = Image.open('IMG_2292.HEIC')
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-2-fe47106ce80b> in <module>
----> 1 img = Image.open('IMG_2292.HEIC')
~/.env/py3/lib/python3.7/site-packages/PIL/Image.py in open(fp, mode)
2685 warnings.warn(message)
2686 raise IOError("cannot identify image file %r"
-> 2687 % (filename if filename else fp))
2688
2689 #
OSError: cannot identify image file 'IMG_2292.HEIC'
似乎请求了对 python-pillow 的支持 (#2806),但那里存在许可/专利问题阻止它。
ImageMagick + Wand
看来ImageMagick 可能是一个选项。在做了brew install imagemagick 和pip install wand 之后,我没有成功。
$ ipython
Python 3.7.0 (default, Oct 2 2018, 09:20:07)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from wand.image import Image
In [2]: with Image(filename='img.jpg') as img:
...: print(img.size)
...:
(4032, 3024)
In [3]: with Image(filename='img.HEIC') as img:
...: print(img.size)
...:
---------------------------------------------------------------------------
MissingDelegateError Traceback (most recent call last)
<ipython-input-3-9d6f58c40f95> in <module>
----> 1 with Image(filename='ces2.HEIC') as img:
2 print(img.size)
3
~/.env/py3/lib/python3.7/site-packages/wand/image.py in __init__(self, image, blob, file, filename, format, width, height, depth, background, resolution, pseudo)
4603 self.read(blob=blob, resolution=resolution)
4604 elif filename is not None:
-> 4605 self.read(filename=filename, resolution=resolution)
4606 # clear the wand format, otherwise any subsequent call to
4607 # MagickGetImageBlob will silently change the image to this
~/.env/py3/lib/python3.7/site-packages/wand/image.py in read(self, file, filename, blob, resolution)
4894 r = library.MagickReadImage(self.wand, filename)
4895 if not r:
-> 4896 self.raise_exception()
4897
4898 def save(self, file=None, filename=None):
~/.env/py3/lib/python3.7/site-packages/wand/resource.py in raise_exception(self, stacklevel)
220 warnings.warn(e, stacklevel=stacklevel + 1)
221 elif isinstance(e, Exception):
--> 222 raise e
223
224 def __enter__(self):
MissingDelegateError: no decode delegate for this image format `HEIC' @ error/constitute.c/ReadImage/556
还有其他可用于以编程方式进行转换的替代方法吗?
【问题讨论】:
-
类似地,Sindre Sorhus 有一个出色的 HEIC 转换器来生成 JPEG 或 PNG 图像,但不是我正在寻找的灵活性。 sindresorhus.com/heic-converter
-
ExifTool 提供了一个用于处理图像元数据并支持 HEIF 的 CLI。应该很容易用 Python 包装。
-
这可能会有所帮助...stackoverflow.com/a/54558699/2836621
标签: python python-3.x heif heic