Python uses the pyexiv2 library to operate images.

Source: Internet
Author: User
Tags in python

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.