Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python检索图像描述(不通过PIL或exifread在EXIF数据中返回)_Python_Image_Exif - Fatal编程技术网

使用Python检索图像描述(不通过PIL或exifread在EXIF数据中返回)

使用Python检索图像描述(不通过PIL或exifread在EXIF数据中返回),python,image,exif,Python,Image,Exif,我想获取.jpg图像的描述。它将显示在MacOSX信息(cmd+i)窗口中 我想阅读“描述”(德语为Beschreibung)字段。与PIL和exifread一起尝试此操作将不会成功 我可以得到这些exif标签,但它们不包括描述或关键字 Key: Image ImageWidth Key: Image ImageLength Key: Image Compression Key: Image Make Key: Image Model Key: Image XResolution Key: I

我想获取.jpg图像的描述。它将显示在MacOSX信息(cmd+i)窗口中

我想阅读“描述”(德语为Beschreibung)字段。与PIL和exifread一起尝试此操作将不会成功

我可以得到这些exif标签,但它们不包括描述或关键字

Key: Image ImageWidth
Key: Image ImageLength
Key: Image Compression
Key: Image Make
Key: Image Model
Key: Image XResolution
Key: Image YResolution
Key: Image PlanarConfiguration
Key: Image ResolutionUnit
Key: Image Software
Key: Image DateTime
Key: Image YCbCrPositioning
Key: Image ExifOffset
Key: Thumbnail Compression
Key: Thumbnail Orientation
Key: Thumbnail XResolution
Key: Thumbnail YResolution
Key: Thumbnail ResolutionUnit
Key: Thumbnail DateTime
Key: Thumbnail JPEGInterchangeFormat
Key: Thumbnail JPEGInterchangeFormatLength
Key: EXIF ExposureTime
Key: EXIF FNumber
Key: EXIF ExposureProgram
Key: EXIF ISOSpeedRatings
Key: EXIF SensitivityType
Key: EXIF ExifVersion
Key: EXIF DateTimeOriginal
Key: EXIF DateTimeDigitized
Key: EXIF ComponentsConfiguration
Key: EXIF ShutterSpeedValue
Key: EXIF ApertureValue
Key: EXIF ExposureBiasValue
Key: EXIF MaxApertureValue
Key: EXIF MeteringMode
Key: EXIF LightSource
Key: EXIF Flash
Key: EXIF FocalLength
Key: EXIF SubSecTimeOriginal
Key: EXIF SubSecTimeDigitized
Key: EXIF ColorSpace
Key: EXIF ExifImageWidth
Key: EXIF ExifImageLength
Key: EXIF FocalPlaneXResolution
Key: EXIF FocalPlaneYResolution
Key: EXIF FocalPlaneResolutionUnit
Key: EXIF SensingMethod
Key: EXIF FileSource
Key: EXIF SceneType
Key: EXIF CVAPattern
Key: EXIF CustomRendered
Key: EXIF ExposureMode
Key: EXIF WhiteBalance
Key: EXIF DigitalZoomRatio
Key: EXIF FocalLengthIn35mmFilm
Key: EXIF SceneCaptureType
Key: EXIF GainControl
Key: EXIF Contrast
Key: EXIF Saturation
Key: EXIF Sharpness
Key: EXIF SubjectDistanceRange
Key: EXIF BodySerialNumber
Key: EXIF LensSpecification
Key: EXIF LensModel

我假设这些字段不是EXIF数据?为了获得描述,我需要查找什么?

通常,元数据可能隐藏在许多地方,您可以使用一些技术来查找它。我将在我的答案的不同部分分别写上几句话


您查找的数据可能位于文件的“扩展属性”中,或者
xattr
。如果在Terminal中运行以下命令,则可以看到这一点:

ls -l image.jpg

-rw-r--r--@ 1 mark  staff  214557  2 Jan 15:47 image.jpg
任何具有“扩展属性”的文件在其权限之后都将具有
@
。然后,您可以通过以下方式查看扩展属性:

ls -@l image.jpg

-rw-r--r--@ 1 mark  staff  2219100  3 Jan 18:07 image.jpg
    com.apple.lastuseddate#PS        16 
    com.apple.quarantine         22 
请参阅手册页,其中包含
man xattr


数据可能位于文件的
EXIF
部分。EXIF数据的最佳工具是
exiftool
,请尝试:

exiftool -v -v image.jpg 
您可以使用自制macOS上安装
exiftool
,方法如下:

brew install exiftool

当你在Mac上创建文件时,苹果会做各种各样的索引,这样数据就可以在元数据数据库中。您可以像这样使用
mdls
进行检查:

mdls image.jpg
样本输出

_kMDItemDisplayNameWithExtensions      = "image.jpg"
kMDItemAcquisitionMake                 = "Apple"
kMDItemAcquisitionModel                = "iPhone 4"
kMDItemAltitude                        = 1.128681019549616
kMDItemAperture                        = 2.970853573907009
kMDItemBitsPerSample                   = 32
kMDItemColorSpace                      = "RGB"
kMDItemContentCreationDate             = 2013-03-09 08:59:50 +0000
kMDItemContentCreationDate_Ranking     = 2013-03-09 00:00:00 +0000
kMDItemContentModificationDate         = 2013-03-09 08:59:50 +0000
kMDItemContentModificationDate_Ranking = 2013-03-09 00:00:00 +0000
kMDItemContentType                     = "public.jpeg"
kMDItemContentTypeTree                 = (
    "public.jpeg",
    "public.image",
    "public.data",
    "public.item",
    "public.content"
)
kMDItemCreator                         = "6.1.2"
kMDItemDateAdded                       = 2021-01-03 18:07:34 +0000
kMDItemDateAdded_Ranking               = 2021-01-03 00:00:00 +0000
kMDItemDisplayName                     = "image.jpg"
kMDItemDocumentIdentifier              = 0
kMDItemEXIFVersion                     = "2.2.1"
kMDItemExposureMode                    = 0
kMDItemExposureProgram                 = 2
kMDItemExposureTimeSeconds             = 0.001094091903719912
kMDItemFlashOnOff                      = 0
kMDItemFNumber                         = 2.8
kMDItemFocalLength                     = 3.85
kMDItemFocalLength35mm                 = 35
kMDItemFSContentChangeDate             = 2021-01-03 18:07:34 +0000
kMDItemFSCreationDate                  = 2021-01-03 18:07:34 +0000
kMDItemFSCreatorCode                   = ""
kMDItemFSFinderFlags                   = 0
kMDItemFSHasCustomIcon                 = (null)
kMDItemFSInvisible                     = 0
kMDItemFSIsExtensionHidden             = 0
kMDItemFSIsStationery                  = (null)
kMDItemFSLabel                         = 0
kMDItemFSName                          = "image.jpg"
kMDItemFSNodeCount                     = (null)
kMDItemFSOwnerGroupID                  = 20
kMDItemFSOwnerUserID                   = 501
kMDItemFSSize                          = 2219100
kMDItemFSTypeCode                      = ""
kMDItemGPSDateStamp                    = "2013:03:09"
kMDItemHasAlphaChannel                 = 0
kMDItemImageDirection                  = 324.4435483870968
kMDItemInterestingDate_Ranking         = 2019-08-27 00:00:00 +0000
kMDItemISOSpeed                        = 80
kMDItemKind                            = "JPEG image"
kMDItemLastUsedDate                    = 2019-08-27 14:26:14 +0000
kMDItemLastUsedDate_Ranking            = 2019-08-27 00:00:00 +0000
kMDItemLatitude                        = 20.84433333333333
kMDItemLogicalSize                     = 2219100
kMDItemLongitude                       = 107.091
kMDItemMeteringMode                    = 5
kMDItemOrientation                     = 0
kMDItemPhysicalSize                    = 2220032
kMDItemPixelCount                      = 5018112
kMDItemPixelHeight                     = 1936
kMDItemPixelWidth                      = 2592
kMDItemProfileName                     = "sRGB IEC61966-2.1"
kMDItemRedEyeOnOff                     = 0
kMDItemResolutionHeightDPI             = 72
kMDItemResolutionWidthDPI              = 72
kMDItemTimestamp                       = "01:59:48"
kMDItemUseCount                        = 1
kMDItemUsedDates                       = (
    "2019-08-26 23:00:00 +0000"
)
kMDItemWhiteBalance                    = 0

你可以尝试一种老式的“暴力”方法。
strings
实用程序搜索任何二进制文件,查找任何看起来像文本的内容,因此您可以尝试提取任何看起来像一串字母的内容,并搜索相对罕见的单词-在您的示例中,我会选择
Wollo
,因此,您可以这样做,以查看字符串
Wollo
是否确实存在于您的文件中,否则它可能位于其他位置:

strings image.jpg | grep "Wollo"

感谢@Mark Serchells answer,我发现描述不是EXIF数据,而是IPTC数据(
exiftool
显示所有字段)

在Python中,我现在可以使用IPTCInfo3:并通过以下方式提取描述:

info = IPTCInfo('img.jpg')
info['caption/abstract']
其中打印:

b'Gel\xe4ndewagen auf den Salzformationen im Salzsee Ass-Ale in der Danakil-Senke, Danakil-Senke, Hamed Ela, Wollo, \xc4thiopien, Afrika

您在exif数据中的何处找到描述字段?@DapperDuck我无法通过PIL或exif read在exif数据中找到它。所以我需要找到它隐藏的地方…谢谢,exiftool显示它是一个IPTCaplication记录。我只是在python中查看exif数据,这就是为什么它没有显示出来的原因,谢谢!这个答案可以帮助未来的观众了解这个问题。将其标记为已接受的答案!感谢您与Stack Overflow社区分享您的发现。继续接受你自己的答案,抓住要点:-)