Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pillow ExifTags Module

Pillow ExifTags Module

ExifTags is a submodule of the Python Pillow image processing library, specifically used to handle EXIF (Exchangeable Image File Format) metadata in images.

EXIF data is extra information embedded in image files, typically including shooting device, shooting parameters (such as aperture, shutter speed), GPS location, etc.

Import ExifTags:

from PIL import Image, ExifTags


Why You Need the ExifTags Module

  1. Read image metadata: Understand the shooting information and properties of an image
  2. Image processing: Perform image processing based on EXIF data (such as auto-rotation)
  3. Data analysis: Batch analyze the shooting parameters of photos
  4. Privacy Protection: View or delete sensitive location information

Main Methods of the ExifTags Module

The following table lists the most commonly used methods and properties of the ExifTags module:

Method/attribute Description Return value type Example
TAGS A dictionary containing mappings of all standard EXIF tag IDs to names dict {256: 'ImageWidth', 257: 'ImageLength'}
GPSTAGS A dictionary containing mappings of all GPS-related EXIF tag IDs to names dict {0: 'GPSVersionID', 1: 'GPSLatitudeRef'}
get(tag_id) Get the tag name by tag ID str ExifTags.get(36867) → 'DateTimeOriginal'
getid(tag_name) Get the tag ID by tag name int ExifTags.getid('DateTimeOriginal') → 36867

Usage examples

1. View All EXIF Tags

Example

from

PIL

import

Image

,

ExifTags

Print all standard EXIF tags

for

tag_id

in

ExifTags.

TAGS

:

    tag_name

=

ExifTags.

TAGS

[

tag_id

]

   

print

(

f

"ID: {tag_id}, Name: {tag_name}"

)

2. Read the EXIF Data of an Image

Example

from

PIL

import

Image

,

ExifTags

image

=

Image.

open

(

"example.jpg"

)

Get the EXIF data

exif_data

=

image._getexif

(

)

if

exif_data:

   

for

tag_id

,

value

in

exif_data.

items

(

)

:

       

Get the tag name

        tag_name

=

ExifTags.

TAGS

.

get

(

tag_id

,

tag_id

)

       

print

(

f

"{tag_name:25}: {value}"

)

else

:

   

print

(

"This image has no EXIF data"

)

3. Handle GPS Information

Example

from

PIL

import

Image

,

ExifTags

image

=

Image.

open

(

"with_gps.jpg"

)

exif_data

=

image._getexif

(

)

if

exif_data:

   

Get the GPS information

    gps_info

=

{

}

   

for

tag_id

in

exif_data:

        tag_name

=

ExifTags.

TAGS

.

get

(

tag_id

,

tag_id

)

       

if

tag_name

==

"GPSInfo"

:

           

for

gps_tag_id

in

exif_data

[

tag_id

]

:

                gps_tag_name

=

ExifTags.

GPSTAGS

.

get

(

gps_tag_id

,

gps_tag_id

)

                gps_info

[

gps_tag_name

]

=

exif_data

[

tag_id

]

[

gps_tag_id

]

   

   

print

(

"GPS Info:"

,

gps_info

)


Common EXIF Tags

The following are several commonly used EXIF tags and their IDs:

Tag ID Tag name Description
271 Make Camera manufacturer
272 Model Camera model
274 Orientation Image orientation
282 XResolution Horizontal resolution
283 YResolution Vertical resolution
296 ResolutionUnit Resolution unit
306 DateTime Date and time of modification
36867 DateTimeOriginal Original capture time
36868 DateTimeDigitized Digitization time
33434 ExposureTime Exposure time
33437 FNumber Aperture value

Notes

  1. Not all images have EXIF data: Some images may have been processed, with EXIF information removed
  2. Privacy Issues: GPS location information may leak privacy, consider deleting it before sharing images
  3. Write Limitations: Pillow has limited support for writing EXIF data, other libraries may be needed for complex operations
  4. Data Type: Values in EXIF data may be of various types (strings, tuples, rationals, etc.)

By mastering the ExifTags module, you can easily access and process metadata in images, providing more possibilities for image processing and analysis.