-3
from PIL import Image
from PIL.ExifTags import TAGS

# path to the image or video
imagename = "image.jpg"

# read the image data using PIL
image = Image.open(imagename)

# extract EXIF data
exifdata = image.getexif()

# iterating over all EXIF data fields
for tag_id in exifdata:
    # get the tag name, instead of human unreadable tag id
    tag = TAGS.get(tag_id, tag_id)
    data = exifdata.get(tag_id)
    # decode bytes 
    if isinstance(data, bytes):
        data = data.decode()
    print(f"{tag:25}: {data}")

ExifVersion : 0220 ComponentsConfiguration : ShutterSpeedValue : (1345, 100) DateTimeOriginal : 2020:08:27 09:43:15 DateTimeDigitized : 2020:08:27 09:43:15 ApertureValue : (185, 100) BrightnessValue : (930, 100) ExposureBiasValue : (0, 10) MaxApertureValue : (185, 100) MeteringMode : 2 Flash : 0 FocalLength : (358, 100) UserComment : ColorSpace : 1 ExifImageWidth : 4128 SceneCaptureType : 0 SubsecTime : 0424 SubsecTimeOriginal : 0424 SubsecTimeDigitized : 0424 ExifImageHeight : 1908 ImageLength : 1908 Make : samsung Model : SM-M305F Orientation : 6 YCbCrPositioning : 1 ExposureTime : (1, 2786) ExifInteroperabilityOffset: 944 XResolution : (72, 1) FNumber : (190, 100) SceneType : YResolution : (72, 1) ImageUniqueID : E13LLLI00PM E13LLMK03PA

ExposureProgram : 2 CustomRendered : 0 ISOSpeedRatings : 40 ResolutionUnit : 2 ExposureMode : 0 FlashPixVersion : 0100 ImageWidth : 4128 WhiteBalance : 0 Software : M305FDDU5CTF2 DateTime : 2020:08:27 09:43:15 DigitalZoomRatio : (0, 0) FocalLengthIn35mmFilm : 27 Contrast : 0 Saturation : 0 Sharpness : 0 ExifOffset : 226 MakerNote : 0100 Z@P

srini
  • 1
  • 1
  • 4
  • What is your question/problem? – buran Sep 29 '20 at 06:03
  • I was Unable get coordinate point details( Latitude and longitude from the image) – srini Sep 29 '20 at 06:06
  • Does this answer your question? [In Python, how do I read the exif data for an image?](https://stackoverflow.com/questions/4764932/in-python-how-do-i-read-the-exif-data-for-an-image) – buran Sep 29 '20 at 06:11
  • I have tried the same getting information about other details except lat and long coordinates – srini Sep 29 '20 at 06:21
  • Does the image has geolocation info? – buran Sep 29 '20 at 06:33
  • 1
    In general: before posting your code-snippet describe the problem you are having. Use the code to show what you already tried. In that way people are not left guessing to what help you are asking for. – Nemelis Sep 29 '20 at 06:36
  • If there is GPS information it should be there under the tag `GPSInfo`, which gives a dict with Northing, Easting, etc. Using the answer below gives more human readable tags for the GPSInfo. – Bruno Vermeulen Sep 29 '20 at 06:49

1 Answers1

2

Using the module piexif (pip install piexif) you can get to the GPS information in the exif as follows.

from pprint import pprint
from PIL import Image
import piexif

codec = 'ISO-8859-1'  # or latin-1

def exif_to_tag(exif_dict):
    exif_tag_dict = {}
    thumbnail = exif_dict.pop('thumbnail')
    exif_tag_dict['thumbnail'] = thumbnail.decode(codec)

    for ifd in exif_dict:
        exif_tag_dict[ifd] = {}
        for tag in exif_dict[ifd]:
            try:
                element = exif_dict[ifd][tag].decode(codec)

            except AttributeError:
                element = exif_dict[ifd][tag]

            exif_tag_dict[ifd][piexif.TAGS[ifd][tag]["name"]] = element

    return exif_tag_dict

def main():
    filename = 'IMG_2685.jpg'  # obviously one of your own pictures
    im = Image.open(filename)

    exif_dict = piexif.load(im.info.get('exif'))
    exif_dict = exif_to_tag(exif_dict)

    pprint(exif_dict['GPS'])

if __name__ == '__main__':
   main()

result

{'GPSAltitude': (94549, 14993),
 'GPSAltitudeRef': 0,
 'GPSDateStamp': '2020:09:04',
 'GPSDestBearing': (1061399, 5644),
 'GPSDestBearingRef': 'T',
 'GPSHPositioningError': (5, 1),
 'GPSImgDirection': (1061399, 5644),
 'GPSImgDirectionRef': 'T',
 'GPSLatitude': ((12, 1), (34, 1), (1816, 100)),
 'GPSLatitudeRef': 'N',
 'GPSLongitude': ((99, 1), (57, 1), (4108, 100)),
 'GPSLongitudeRef': 'E',
 'GPSSpeed': (0, 1),
 'GPSSpeedRef': 'K',
 'GPSTimeStamp': ((13, 1), (2, 1), (4599, 100)),
 'GPSVersionID': (2, 2, 0, 0)}

Here exif_to_tag translates exif codes to more readable tags.

Bruno Vermeulen
  • 2,567
  • 2
  • 13
  • 23
  • TypeError: 'NoneType' object is not subscriptable – srini Sep 29 '20 at 06:30
  • AttributeError: 'bytes' object has no attribute 'encode' – srini Sep 29 '20 at 06:33
  • What Python version are you running? The code runs just fine in Python 3.7 and up. In what lines do you get the error codes? – Bruno Vermeulen Sep 29 '20 at 06:37
  • I am using 3.7.6 version. getting error in the line number 30 and 31 as AttributeError: 'bytes' object has no attribute 'encode' – srini Sep 29 '20 at 07:03
  • ok, please remove the line `exif_tag_dict['thumbnail'] = thumbnail.decode(codec)` from the function `exif_to_tag` and see if it runs... – Bruno Vermeulen Sep 29 '20 at 07:09
  • Tried removing the mentioned line from the code but still throwing the same error AttributeError: 'bytes' object has no attribute 'encode' – srini Sep 29 '20 at 07:16
  • I suspect though you will get an empty dict for `exif_dict['GPS'] as otherwise you would have got GPSInfo in your original code. – Bruno Vermeulen Sep 29 '20 at 07:16
  • If the image has no exif data `piexif.load` will not work. However as in your questions there is obviously some exif data, so unless I get image I cannot investigate further. – Bruno Vermeulen Sep 29 '20 at 07:27
  • If possible can you please share your image so that I will cross check – srini Sep 29 '20 at 07:29
  • ok try this link: https://1drv.ms/u/s!ApHNZgZdMB7wiLRNOnLthD5YeQwAEw?e=ac7rL0 – Bruno Vermeulen Sep 29 '20 at 07:39
  • Tried with the shared image but still came across the same above mentioned error – srini Sep 29 '20 at 07:50
  • Then you have not copied the code correctly. I suggest to copy and paste it again. Also make sure you have pip installed `piexif`. – Bruno Vermeulen Sep 29 '20 at 07:52
  • I have installed it correctly as well as pasted the code properly still the issue is there – srini Sep 29 '20 at 07:55
  • well, something is not right at your end because it is not running, is it. It is running ok here so not much else I can do than to suggest to start with a clean slate and copy and paste the code again. – Bruno Vermeulen Sep 29 '20 at 07:59
  • How to altitude and gps satellite info using python – srini Oct 13 '20 at 13:00