Pyexiv2 is the Python binding of the exiv2 library, and exiv2 is the C ++ library used to operate the EXIF, IPTC, and XMP image metadata.
For pyexiv2, see its official website: http://tilloy.net/dev/pyexiv2/
Currently, pyexiv2 does not seem to support Python3. This article uses Python2.7 as an example.
The command to install pyexiv2 in Ubuntu is: apt-get install python-pyexiv2
When using pyexiv2 in Python, pay attention to the following points:
1. import pyexiv2
2. Get the metadata Object: pyexiv2.ImageMetadata (image-file)
3. metadata. read () is used to read the metadata of an image.
4. metadata. write () is used to write the metadata of the image back.
5. metadata [key] = value can be used to create or modify a key-value pair in the original data.
The sample code for using pyexiv2 is as follows:
#! /Usr/bin/python2.7
# Just a sample for using python-pyexiv2 Library
# Pyexiv2 is a Python binding to exiv2,
# The C ++ library for manipulation of EXIF, IPTC and XMP image metadata.
Import pyexiv2
Import OS
Path = '/home/master/Pictures/wall-paper'
For file in OS. listdir (path ):
Print 'File: '+ OS. path. join (path, file)
Print '--------------------------------------------------------------'
Metadata = pyexiv2.ImageMetadata (OS. path. join (path, file ))
Metadata. read ()
Print metadata ['exif. Image. Datetime']. value. strftime ('% A % d % B % Y, % H: % M: % S ')
Print metadata ['exif. Image. Imagedescription']. value
Print metadata ['exif. Image. Soft']. value
Print metadata ['exif. Image. Existtag']. value
Key = 'exif. Photo. Usercomment'
Value = 'A comment .'
Metadata [key] = pyexiv2.ExifTag (key, value)
# Metadata [key] = value # this a shotcut method as the previous line.
Metadata. write ()
Print metadata [key]. value
Metadata [key]. value = 'a new comment .'
Metadata. write ()
Print metadata [key]. value
Print '--------------------------------------------------------------'
The running result on one of my systems is as follows:
Master @ jay-linux :~ /Workspace/python $./pyexiv2-sample.py
File:/home/master/Pictures/wall-paper/855402454225855163.jpg
---------------------
Sunday 17 10000l 2011, 23:59:29
OLYMPUS DIGITAL CAMERA
Adobe Photoshop CS4 Windows
992
A comment.
A new comment
---------------------
File:/home/master/Pictures/wall-paper/723672165124933357.jpg
---------------------
Sunday 17 10000l 2011, 23:55:11
OLYMPUS DIGITAL CAMERA
Adobe Photoshop CS4 Windows
992
A comment.
A new comment
---------------------
In addition, you can try PIL (Python Imaging Library), but it seems that PIL is not as powerful as pyexiv2.