Article directory
- Introduction
-
- Image metadata
- How to view image metadata
- pyexiv2
- Function interface for pyexiv2 to read image metadata
- EXIF metadata description of main part pyexiv2
- code example
- References
Introduction
We can use pyexiv2
, a Python third-party tool, to read the metadata of image files in Windows systems. Among them, the commonly used image metadata is generally EXIF data about image formats and IPTC data about copyright. This blog introduces the installation and basic usage of pyexiv2
, and provides Python code examples. Of course, pillow can also view the metadata of images. The article also directly gives code examples for pillow to view image metadata.
PS: If the image data has been erased with metadata information, pyexiv2 may not be able to read any data!
For example, in the following line of pictures in the COCO data, the relevant information cannot be read. It is recommended to try it with a photo you took yourself.
Image metadata
Image metadata refers to additional information stored in digital image files that describes various properties and characteristics about the image. This metadata provides detailed information about the image, making it easier to organize, search, and understand the image content. By using tools such as Pyexiv2, users can easily access and edit this metadata to meet various needs. Common image metadata includes:
-
EXIF (Exchangeable Image File Format) data?: This is one of the most common image metadata, including shooting date and time, camera model, exposure time, aperture value, ISO sensitivity, focal length, Flash status and other information related to shooting conditions.
-
IPTC (International Press Telecommunications Council) data: usually contains descriptive information about the image, such as title, author, copyright, keywords, and special notes.
-
XMP (Extensible Metadata Platform) data: Provides a flexible way to store and manage image metadata, including custom tags, extended attributes, and additional information.
-
GPS information: If the GPS function is enabled, image metadata can include the latitude and longitude coordinates of the shooting location, allowing users to accurately know the geographical location where the image was captured.
-
Color space information: describes the color space of the image, such as sRGB, Adobe RGB, etc.
-
File format and compression parameters: Specify the file format (such as JPEG, PNG) and compression settings of the image.
-
Image resolution: The width and height of the image and pixel density information.
-
Modification History: Contains information about image edits and modifications, such as editing software, editing date, and history.
How to view image metadata
Method 1: View the image properties in Windows and select Details
.
Method 2: Import pictures into Photoshop, shortcut keys Ctrl + Alt + Shift + I
pyexiv2
Pyexiv2 is a Python third-party library for processing image metadata. It allows you to easily read and edit metadata information of photos and image files, such as EXIF (Exchangeable Image File Format) data, IPTC (International Press Telecommunications Council) tags and XMP (Extensible Metadata Platform) attributes. This metadata contains important information about the image, such as shooting date, camera model, GPS coordinates and author information.
Pyexiv2 provides a simple yet powerful interface that allows you to extract information about the image from the image file, such as shooting date, camera model, aperture, shutter speed, etc. You can also use it to add, modify, or remove an image’s metadata to suit your specific needs. This is very important in image processing and management because it helps you automate and personalize the image processing process.
In summary, Pyexiv2 is a metadata processing library for Python that provides convenient tools for accessing and editing metadata information in image files, making image processing and management more flexible and efficient. Whether you’re working with photos, building image-related applications, or managing digital archives, Pyexiv2 is a useful tool.
Function interface for pyexiv2 to read image metadata
- read_exif
- read_comment
- read_icc
- read_iptc
- read_raw_xmp
- read_thumbnail
- read_xmp
with pyexiv2.Image(filename="000000000009.jpg") as img: exif = img.read_exif() comment = img.read_comment() icc = img.read_icc() iptc = img.read_iptc() raw_xmp = img.read_raw_xmp() thumbnail = img.read_thumbnail() xmp = img.read_xmp()
EXIF metadata description of main part pyexiv2
These are keys to the metadata dictionary that may be returned by calling read_exif
.
Metadata | Description |
---|---|
Exif.Image.ImageWidth | Image width |
Exif.Image.Model | Photography equipment and model |
Exif.Image.Orientation | Image Orientation |
Exif.Image.DateTime | Taking Date |
Exif.Image.YCbCrPositioning | Hue Positioning |
Exif.Photo.ExposureTime | Exposure Time |
Exif.Photo.FocalLength | Focal length |
Exif.Photo.LightSource | Light source |
Exif.Photo.BrightnessValue | Brightness |
Exif.Photo.ColorSpace | Color space |
Exif.Image.XResolution | X direction resolution |
Exif.Image.YResolution | Y direction resolution |
There are many related parameters, so I won’t list them all in the table. If you are interested, you can check it out by yourself!
Code example
Read the image EXIT metadata, modify the image EXIT metadata, and call pillow to view the image metadata.
yifeiliu.jpg
import pyexiv2 from PIL import Image def read_exif_metadata(img_path): """Call pyexiv2 to read the EXIF metadata of the image and return a data dictionary""" with pyexiv2.Image(filename=img_path, encoding="utf-8") as pyexiv2_img: exif_metadata = pyexiv2_img.read_exif() return exif_metadata def set_exif_metadata(img_path, metadata_dict: dict): """Calling pyexiv2 to modify the EXIF metadata of the image, returning True if successful, and False if failed""" try: with pyexiv2.Image(filename=img_path, encoding="utf-8") as pyexiv2_img: pyexiv2_img.modify_exif(metadata_dict) return True except Exception as e: print(e) return False def read_exif_metadata_PIL(img_path): """Call pillow to read the EXIF metadata of the image and return a data dictionary""" with Image.open(fp=img_path) as pillow_img: exif_metadata = pillow_img.getexif() return exif_metadata def clear_exif_metadata(img_path): """Calling pyexiv2 to clear the EXIF metadata of the image, returning True if successful, and False if failed""" try: with pyexiv2.Image(filename=img_path, encoding="utf-8") as pyexiv2_img: pyexiv2_img.clear_exif() return True except Exception as e: print(e) return False if __name__ == "__main__": metadata = read_exif_metadata("yifeiliu.jpg") print(f"Reading EXIF metadata for the first time: {<!-- -->metadata}") metadata_dict = {<!-- --> "Exif.Image.Model": "Cannon R3", "Exif.Image.ImageWidth": 1202, "Exif.Image.ImageLength": 676 } ret = set_exif_metadata("yifeiliu.jpg", metadata_dict) metadata = read_exif_metadata("yifeiliu.jpg") print(f"Reading EXIF metadata for the second time: {<!-- -->metadata}") metadata = read_exif_metadata_PIL("yifeiliu.jpg") print(f"Reading EXIF metadata for the third time (pillow) {<!-- -->metadata}") ret = clear_exif_metadata("yifeiliu.jpg") metadata = read_exif_metadata("yifeiliu.jpg") print(f"Reading EXIF metadata for the third time (after clearing data): {<!-- -->metadata}")
result:
Reference materials
- Github:pyexiv2
- pyexiv2 Chinese tutorial
- Welcome to py3exiv2’s documentation!
- CSDN: Python uses pyexiv2 to read and write image metadata (EXIF, IPTC, XMP)
- https://www.osgeo.cn/python-tutorial/imgs-exif.html
It is not easy to collect, organize and create. If it helps, please help Like
?Collect
, thank you!