How to get Exif information of photos in Python3?

The thing is like this, haven't I recently changed to a new mobile phone? I exported the photos from the old mobile phone to my computer to make a backup, but all the photos were backed up to a directory, which was obviously messy and difficult to find. According to my past practice, they are stored in directory format named by year/month, so this leads me to how to use a program to process these photos.

When I was playing SLR before, I learned that digital cameras have some standard protocols and formats. One of the Exifformats can store the shooting time of the photos. By getting the shooting time and Pythonprogram, these photos can be returned to their original location.

What is Exif

ExifThe Exchangeable image file format is specially designed for digital camera photos and can record the attribute information and shooting data of digital photos.

ExifInformation is a set of shooting parameters contained in the image file format. The Exif recorded metadata information is very rich, mainly including the following types of information:

  • Shooting date
  • Photography equipment (body, lens, flash, etc.)
  • Shooting parameters (shutter speed, aperture F value, ISO speed, focal length, metering mode, etc.)
  • Image processing parameters (sharpening, contrast, saturation, white balance, etc.)
  • Image description and copyright information
  • GPS location data
  • thumbnail

Sample code:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import exifread


def main(image):

    with open(image, 'rb') as f:

        exif = exifread.process_file(f)

        # 设备信息
        print('相机品牌:', exif['Image Make'])
        print('相机型号:', exif['Image Model'])

        # 相片信息
        print('拍摄时间:', exif['Image DateTime'])
        print('图片大小:', exif['EXIF ExifImageLength'], '*', exif['EXIF ExifImageWidth'])

        # 位置信息
        lng = f"{
      
      exif['GPS GPSLongitudeRef']}{
      
      exif['GPS GPSLongitude']}"
        lat = f"{
      
      exif['GPS GPSLatitudeRef']}{
      
      exif['GPS GPSLatitude']}"
        print('经纬度:', lng, lat)


if __name__ == '__main__':
    main('IMG_9871.jpeg')

operation result:

相机品牌: Apple
相机型号: iPhone XS Max
拍摄时间: 2023:04:06 18:32:56
图片大小: 3024 * 4032
经纬度: E[116, 29, 2391/100] N[40, 2, 481/50]

Of course, Exifthe information contained is far more than this. For specific parameters required, please refer to the following articles:
https://juejin.cn/post/6844904033027620878
https://www.biaodianfu.com/exif-python.html

Guess you like

Origin blog.csdn.net/yilovexing/article/details/133268741