gpt4 book ai didi

python - 如何在 Python 中使用 HEIC 图像文件类型

转载 作者:太空狗 更新时间:2023-10-30 00:12:31 39 4
gpt4 key购买 nike

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 + 魔杖

看起来 ImageMagick可能是一个选择。在执行了 brew install imagemagickpip 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

还有其他可用于以编程方式进行转换的替代方法吗?

最佳答案

你们应该看看这个库,它是 libheif 的 Python 3 包装器库,它应该服务于您的文件转换、提取元数据的目的:

https://github.com/david-poirier-csn/pyheif

https://pypi.org/project/pyheif/

示例用法:

 import io

import whatimage
import pyheif
from PIL import Image


def decodeImage(bytesIo):

fmt = whatimage.identify_image(bytesIo)
if fmt in ['heic', 'avif']:
i = pyheif.read_heif(bytesIo)

# Extract metadata etc
for metadata in i.metadata or []:
if metadata['type']=='Exif':
# do whatever

# Convert to other file format like jpeg
s = io.BytesIO()
pi = Image.frombytes(
mode=i.mode, size=i.size, data=i.data)

pi.save(s, format="jpeg")

...

关于python - 如何在 Python 中使用 HEIC 图像文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54395735/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com