我使用以下代码从JPG和HEIC文件中提取GPS位置:
#coding=utf-8
from PIL import Image
from urllib.request import urlopen
from PIL.ExifTags import TAGS
from PIL.ExifTags import GPSTAGS
from pillow_heif import register_heif_opener
def get_exif(filename):
    image = Image.open(filename)
    image.verify()
    return image._getexif()
def get_geotagging(exif):
    if not exif:
        raise ValueError("No EXIF metadata found")
    geotagging = {}
    for (idx, tag) in TAGS.items():
        if tag == 'GPSInfo':
            if idx not in exif:
                raise ValueError("No EXIF geotagging found")
            for (key, val) in GPSTAGS.items():
                if key in exif[idx]:
                    geotagging[val] = exif[idx][key]
    return geotagging
register_heif_opener()
my_image='IMG_9610.HEIC'
#my_image='IMG_20210116_215317.jpg'
exif = get_exif(my_image)
labeled = get_geotagging(exif)
print(labeled)此代码适用于JPEG文件,但在HEIC中返回以下错误:
AttributeError: _getexif如果我添加以下函数
def get_labeled_exif(exif):
    labeled = {}
    for (key, val) in exif.items():
        labeled[TAGS.get(key)] = val
    return labeled并将'_getexif()‘替换为'getexif()’,然后对这两个文件都有效,但是数据是加密的-- 'GPSInfo': 1234和get_geotagging()对这类exif不起作用。
我怎么才能修好它?
发布于 2022-06-11 12:52:48
更新后06-12-2022
下面的代码能够从我的系统上的HEIC图像文件中提取GEO标记信息。
from PIL import Image
from pillow_heif import register_heif_opener
def get_exif(filename):
    image = Image.open(filename)
    image.verify()
    return image.getexif().get_ifd(0x8825)
def get_geotagging(exif):
    geo_tagging_info = {}
    if not exif:
        raise ValueError("No EXIF metadata found")
    else:
        gps_keys = ['GPSVersionID', 'GPSLatitudeRef', 'GPSLatitude', 'GPSLongitudeRef', 'GPSLongitude',
                    'GPSAltitudeRef', 'GPSAltitude', 'GPSTimeStamp', 'GPSSatellites', 'GPSStatus', 'GPSMeasureMode',
                    'GPSDOP', 'GPSSpeedRef', 'GPSSpeed', 'GPSTrackRef', 'GPSTrack', 'GPSImgDirectionRef',
                    'GPSImgDirection', 'GPSMapDatum', 'GPSDestLatitudeRef', 'GPSDestLatitude', 'GPSDestLongitudeRef',
                    'GPSDestLongitude', 'GPSDestBearingRef', 'GPSDestBearing', 'GPSDestDistanceRef', 'GPSDestDistance',
                    'GPSProcessingMethod', 'GPSAreaInformation', 'GPSDateStamp', 'GPSDifferential']
        for k, v in exif.items():
            try:
                geo_tagging_info[gps_keys[k]] = str(v)
            except IndexError:
                pass
        return geo_tagging_info
register_heif_opener()
my_image = 'IMG_8362.heic'
image_info = get_exif(my_image)
results = get_geotagging(image_info)
print(results)
# x used to mask data
{'GPSLatitudeRef': 'N', 
'GPSLatitude': '(3x.0, 5x.0, 1x.0x)', 
'GPSLongitudeRef': 'W', 
'GPSLongitude': '(8x.0, 2x.0, 5x.2x)', 
'GPSAltitudeRef': "b'\\x00'", 
'GPSAltitude': '279.63243243243244', 
'GPSSpeedRef': 'K', 
'GPSSpeed': '0.04649941997239198', 
'GPSImgDirectionRef': 'T', 
'GPSImgDirection': '274.37165833514456', 
'GPSDestBearingRef': 'T', 
'GPSDestBearing': '27x.37165833514456', 
'GPSDateStamp': '2022:06:12'}----------------------------------------
My system information
----------------------------------------
Platform:     Apple
OS Version:   macOS Catalina 10.15.7
Python: 3.9
Pillow: 9.1.1
pillow_heif: 0.3.0
----------------------------------------原稿06-11-2022
简单地说,枕头目前不支持高效率图像格式(HEIF)文件格式。
参考资料:
解决这个问题的方法之一是比海夫。这个Python包具有将HEIC图像转换为JPEG图像的功能。在此转换之后,Pillow将能够从图像中读取数据。
解决此格式读取问题的另一个解决方法是吡咯烷酮。下面是我在将回答文件转换为JPEG文件时发布的TIFF,以便与Pillow一起读取。
您也可以使用ExifTool,它可以从盒子中读取HEIC文件。使用它稍微复杂一些,因为它需要使用subprocess。
https://stackoverflow.com/questions/72522522
复制相似问题