How to extract metadata from a image using python?

2019-02-09 21:07发布

问题:

Hi im working on a program that will open an image and then extract the metadata from it How do i extract metadata using python ?

Thanks

回答1:

Use Pillow, it's a fork of PIL that is still in active development, and supports python3. Here I use a dict generator to map the exif data to a dict

from PIL import Image, ExifTags
img = Image.open("/path/to/file.jpg")
exif = { ExifTags.TAGS[k]: v for k, v in img._getexif().items() if k in ExifTags.TAGS }


回答2:

You can use following python code for this.

#!/bin/python
import os
import sys
from PIL import Image
from PIL.ExifTags import TAGS

image = sys.argv[1]

for (tag,value) in Image.open(image)._getexif().iteritems():
        print '%s = %s' % (TAGS.get(tag), value)

Here is the sample output.