### Install dependencies and run tests Source: https://github.com/leohsiao1/pyexiv2/blob/master/pyexiv2/tests/README.md This snippet installs the required testing dependencies using pip and executes the test suite with verbose output enabled. ```sh pip install pytest psutil pytest -sv ``` -------------------------------- ### Install pyexiv2 using pip Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md This command installs the pyexiv2 library using pip. It is recommended for supported platforms as it provides pre-compiled binaries. ```bash pip install pyexiv2 ``` -------------------------------- ### Convert Metadata Formats with pyexiv2 Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Provides examples of converting metadata between EXIF, IPTC, and XMP formats using `pyexiv2`'s conversion functions. It covers converting EXIF to XMP, IPTC to XMP, XMP to EXIF, and XMP to IPTC, showing the input metadata dictionaries and the resulting converted dictionaries. ```python import pyexiv2 # Convert EXIF to XMP exif_data = { 'Exif.Image.Artist': 'John Doe', 'Exif.Image.Rating': '4', 'Exif.Image.ImageDescription': 'A test image' } xmp_result = pyexiv2.convert_exif_to_xmp(exif_data) print(xmp_result) # {'Xmp.dc.creator': ['John Doe'], 'Xmp.xmp.Rating': '4', 'Xmp.dc.description': {...}} # Convert IPTC to XMP iptc_data = { 'Iptc.Application2.ObjectName': 'Photo Title', 'Iptc.Application2.Keywords': ['nature', 'sunset'] } xmp_result = pyexiv2.convert_iptc_to_xmp(iptc_data) print(xmp_result) # Convert XMP to EXIF xmp_data = { 'Xmp.dc.creator': ['Jane Doe'], 'Xmp.xmp.Rating': '5' } exif_result = pyexiv2.convert_xmp_to_exif(xmp_data) print(exif_result) # {'Exif.Image.Artist': 'Jane Doe', 'Exif.Image.Rating': '5'} # Convert XMP to IPTC iptc_result = pyexiv2.convert_xmp_to_iptc(xmp_data) print(iptc_result) ``` -------------------------------- ### Compile pyexiv2 Module (Darwin) Source: https://github.com/leohsiao1/pyexiv2/blob/master/pyexiv2/lib/README.md Compiles the exiv2api.cpp file into a Python extension module (exiv2api.so) using g++ and Pybind11 on Darwin. Requires Exiv2 and Pybind11 to be installed and environment variables set. ```shell cd $LIB_DIR rm -f exiv2api.so g++ exiv2api.cpp -o exiv2api.so \ -std=c++11 -O3 -Wall -shared -fPIC \ `python3.$py_version -m pybind11 --includes` \ -I $EXIV2_DIR/include \ -L $EXIV2_DIR/lib \ -l exiv2 \ -undefined dynamic_lookup ``` -------------------------------- ### Reading Metadata Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Examples of reading EXIF, IPTC, and XMP metadata from an image. These operations are read-only and do not modify the source file. ```python img.read_exif() img.read_iptc() img.read_xmp() ``` -------------------------------- ### Compile pyexiv2 Module (Linux) Source: https://github.com/leohsiao1/pyexiv2/blob/master/pyexiv2/lib/README.md Compiles the exiv2api.cpp file into a Python extension module (exiv2api.so) using g++ and Pybind11 on Linux. Requires Exiv2 and Pybind11 to be installed and environment variables set. ```shell cd $LIB_DIR rm -f exiv2api.so g++ exiv2api.cpp -o exiv2api.so \ -std=c++11 -O3 -Wall -shared -fPIC \ `python3.$py_version -m pybind11 --includes` \ -I $EXIV2_DIR/include \ -L $EXIV2_DIR/lib \ -l exiv2 ``` -------------------------------- ### Troubleshoot missing exiv2.dll on Windows Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md This Python code snippet illustrates a FileNotFoundError on Windows when exiv2.dll is missing or its dependencies are not met. It suggests installing the Microsoft Visual C++ Redistributable. ```python import pyexiv2 # Traceback indicates FileNotFoundError: Could not find module '...\exiv2.dll' ``` -------------------------------- ### Troubleshoot missing libinih.0.dylib on MacOS Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md This Python code snippet addresses an OSError on MacOS where libinih.0.dylib is not found. It recommends installing inih using Homebrew. ```python # Traceback indicates Library not loaded: '/usr/local/opt/inih/lib/libinih.0.dylib' ``` -------------------------------- ### Register Custom XMP Namespaces with pyexiv2 Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Explains how to register custom XMP namespaces using `pyexiv2.registerNs()`, which is necessary for writing non-standard XMP tags. The example demonstrates the error that occurs when attempting to write to an unregistered namespace and the successful modification after registration. ```python import pyexiv2 # Attempting to write to unregistered namespace raises error with pyexiv2.Image('./photo.jpg') as img: try: img.modify_xmp({'Xmp.myns.mytag': 'Hello'}) except RuntimeError as e: print(e) # "No namespace info available for XMP prefix `myns'" # Register namespace first pyexiv2.registerNs('http://example.com/myns/', 'myns') with pyexiv2.Image('./photo.jpg') as img: img.modify_xmp({'Xmp.myns.mytag': 'Hello World'}) print(img.read_xmp()['Xmp.myns.mytag']) # 'Hello World' ``` -------------------------------- ### Copy Metadata Between Images using pyexiv2 Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Illustrates copying metadata (EXIF, IPTC, XMP, comment, ICC, thumbnail) between two images using the `copy_to_another_image` method. It shows how to copy all metadata types or selectively copy specific types. The example includes clearing destination metadata before copying and verifying the copy operation. ```python import pyexiv2 with pyexiv2.Image('./source.jpg') as src: with pyexiv2.Image('./destination.jpg') as dst: # Optionally clear destination first dst.clear_exif() dst.clear_iptc() dst.clear_xmp() # Copy all metadata types src.copy_to_another_image(dst, exif=True, iptc=True, xmp=True, comment=True, icc=True, thumbnail=True ) # Verify copy assert src.read_exif() == dst.read_exif() assert src.read_xmp() == dst.read_xmp() # Copy only specific metadata types with pyexiv2.Image('./source.jpg') as src: with pyexiv2.Image('./destination.jpg') as dst: src.copy_to_another_image(dst, exif=True, iptc=False, xmp=True, comment=False, icc=False, thumbnail=False ) ``` -------------------------------- ### Troubleshoot missing libintl.8.dylib on MacOS Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md This Python code snippet shows an OSError on MacOS related to a missing libintl.8.dylib. The solution involves installing gettext using Homebrew. ```python import pyexiv2 # Traceback indicates Library not loaded: /usr/local/lib/libintl.8.dylib ``` -------------------------------- ### Control Logging Verbosity with pyexiv2.set_log_level() Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Demonstrates how to control the verbosity of Exiv2's logging output using `pyexiv2.set_log_level()`. Different levels (0-4) allow for debugging, informational messages, warnings, errors, or muting all non-error output. The example shows muting messages and then restoring the default level. ```python import pyexiv2 # Log levels: 0=debug, 1=info, 2=warn (default), 3=error, 4=mute pyexiv2.set_log_level(4) # Mute all non-error messages with pyexiv2.Image('./photo.jpg') as img: # This would normally raise an error, but is silenced img.modify_xmp({'Xmp.xmpMM.History': 'type="Seq"'}) # Restore default log level pyexiv2.set_log_level(2) ``` -------------------------------- ### Get Image Properties with pyexiv2 Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Shows how to retrieve basic image properties such as pixel dimensions (width and height), MIME type, and access modes for different metadata types without modifying the image file. This is useful for inspecting image characteristics before or during metadata operations. ```python import pyexiv2 with pyexiv2.Image('./photo.jpg') as img: # Get image dimensions width = img.get_pixel_width() height = img.get_pixel_height() print(f'Image size: {width}x{height}') # e.g., 'Image size: 4000x3000' # Get MIME type mime = img.get_mime_type() print(f'MIME type: {mime}') # 'image/jpeg' # Get access modes for different metadata types access = img.get_access_mode() print(access) # {'exifData': 'read+write', 'iptcData': 'read+write', # 'xmpData': 'read+write', 'comment': 'read+write'} ``` -------------------------------- ### Prepare Exiv2 Libraries (Darwin) Source: https://github.com/leohsiao1/pyexiv2/blob/master/pyexiv2/lib/README.md Copies the Exiv2 dynamic library to the expected location for pyexiv2 compilation on Darwin. Environment variables EXIV2_DIR and LIB_DIR must be set correctly. ```shell EXIV2_DIR=??/exiv2-0.28.7-Darwin-x86_64 LIB_DIR=??/pyexiv2/lib cp ${EXIV2_DIR}/lib/libexiv2.0.28.7.dylib ${LIB_DIR}/libexiv2.dylib ``` -------------------------------- ### Prepare Exiv2 Libraries (Windows) Source: https://github.com/leohsiao1/pyexiv2/blob/master/pyexiv2/lib/README.md Copies the Exiv2 DLL to the expected location for pyexiv2 compilation on Windows. Requires Visual Studio 2022 and environment variables to be set. ```batch "C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" set EXIV2_DIR=??\exiv2-0.28.7-2022msvc-AMD64 set LIB_DIR=??\pyexiv2\lib copy %EXIV2_DIR%\bin\exiv2.dll %LIB_DIR% ``` -------------------------------- ### Prepare Exiv2 Libraries (Linux) Source: https://github.com/leohsiao1/pyexiv2/blob/master/pyexiv2/lib/README.md Copies the Exiv2 shared library to the expected location for pyexiv2 compilation on Linux. Environment variables EXIV2_DIR and LIB_DIR must be set correctly. ```shell EXIV2_DIR=??/exiv2-0.28.7-Linux-x86_64 LIB_DIR=??/pyexiv2/lib/ cp $EXIV2_DIR/lib/libexiv2.so.0.28.7 $EXIV2_DIR/lib/libexiv2.so cp $EXIV2_DIR/lib/libexiv2.so.0.28.7 $LIB_DIR/libexiv2.so ``` -------------------------------- ### Opening Images with pyexiv2 Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Demonstrates how to open image files using the Image class. It covers manual resource management, the recommended context manager approach, and handling non-ASCII file paths. ```python import pyexiv2 # Basic usage with manual close img = pyexiv2.Image('./photo.jpg') exif_data = img.read_exif() print(exif_data) img.close() # Recommended: using context manager for automatic cleanup with pyexiv2.Image('./photo.jpg') as img: exif = img.read_exif() iptc = img.read_iptc() xmp = img.read_xmp() # For non-ASCII paths (e.g., Chinese characters on Windows) with pyexiv2.Image('./照片.jpg', encoding='gbk') as img: data = img.read_exif() ``` -------------------------------- ### Opening and Closing Images Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Demonstrates how to open an image file using the Image class and ensure resources are freed. It shows both manual closing and the use of the 'with' statement for automatic resource management. ```python import pyexiv2 # Manual approach img = pyexiv2.Image(r'.\pyexiv2\tests\1.jpg') data = img.read_exif() img.close() # Context manager approach with pyexiv2.Image(r'.\pyexiv2\tests\1.jpg') as img: data = img.read_exif() ``` -------------------------------- ### Convert EXIF to XMP using pyexiv2 Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Demonstrates how to convert EXIF tags to XMP tags using the pyexiv2 library. This function takes a dictionary of EXIF tags and returns a dictionary of corresponding XMP tags. It relies on the pyexiv2 library and its underlying Exiv2 C++ library. ```python import pyexiv2 # Example usage: # exif_data = {'Exif.Image.Artist': 'test-中文-', 'Exif.Image.Rating': '4'} # xmp_data = pyexiv2.convert_exif_to_xmp(exif_data) # print(xmp_data) # Expected output: {'Xmp.dc.creator': ['test-中文-'], 'Xmp.xmp.Rating': '4'} ``` -------------------------------- ### Download Exiv2 Release (Windows) Source: https://github.com/leohsiao1/pyexiv2/blob/master/pyexiv2/lib/README.md Downloads and extracts the Exiv2 C++ library release for Windows. This is a prerequisite for compiling pyexiv2 on Windows. ```shell curl -O https://github.com/Exiv2/exiv2/releases/download/v0.28.7/exiv2-0.28.7-2022msvc-AMD64.zip python -m zipfile -e exiv2-0.28.7-2022msvc-AMD64.zip . ``` -------------------------------- ### Configure Logging Levels Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Demonstrates how to set the global log level for pyexiv2 to control the verbosity of error reporting and console output. ```python # Set log level to mute (4) pyexiv2.set_log_level(4) img.modify_xmp({'Xmp.xmpMM.History': 'type="Seq"'}) ``` -------------------------------- ### Handle Multi-valued Metadata Tags Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Explains how pyexiv2 handles tags with multiple values as lists and how LangAlt XMP tags are converted to dictionaries. ```python # Multi-value tags img.modify_xmp({'Xmp.dc.subject': ['tag1', 'tag2', 'tag3']}) # LangAlt XMP tags img.read_xmp()['Xmp.dc.title'] ``` -------------------------------- ### Download Exiv2 Release (Linux) Source: https://github.com/leohsiao1/pyexiv2/blob/master/pyexiv2/lib/README.md Downloads and extracts the Exiv2 C++ library release for Linux. This is a prerequisite for compiling pyexiv2 on Linux. ```shell curl -O https://github.com/Exiv2/exiv2/releases/download/v0.28.7/exiv2-0.28.7-Linux-x86_64.tar.gz tar -zxvf exiv2-0.28.7-Linux-x86_64.tar.gz ``` -------------------------------- ### Reading EXIF Metadata Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Explains how to extract EXIF tags from an image. It demonstrates both basic dictionary access and detailed retrieval including type names and tag descriptions. ```python import pyexiv2 with pyexiv2.Image('./photo.jpg') as img: exif = img.read_exif() print(exif.get('Exif.Image.DateTime')) exif_detail = img.read_exif_detail() print(exif_detail['Exif.Image.DateTime']) ``` -------------------------------- ### Download Exiv2 Release (Darwin) Source: https://github.com/leohsiao1/pyexiv2/blob/master/pyexiv2/lib/README.md Downloads and extracts the Exiv2 C++ library release for Darwin (macOS). This is a prerequisite for compiling pyexiv2 on macOS. ```shell curl -O https://github.com/Exiv2/exiv2/releases/download/v0.28.7/exiv2-0.28.7-Darwin-x86_64.tar.gz tar -zxvf exiv2-0.28.7-Darwin-x86_64.tar.gz ``` -------------------------------- ### Compile pyexiv2 Module (Windows) Source: https://github.com/leohsiao1/pyexiv2/blob/master/pyexiv2/lib/README.md Compiles the exiv2api.cpp file into a Python extension module (exiv2api.pyd) using cl.exe and Pybind11 on Windows. Requires Visual Studio 2022, Exiv2, and Pybind11. ```batch cd %LIB_DIR% del exiv2api.pyd cl /MD /LD exiv2api.cpp /EHsc -I %EXIV2_DIR%\include -I %PY_HOME%\include -I %PY_HOME%\Lib\site-packages\pybind11\include /link %EXIV2_DIR%\lib\exiv2.lib %PY_HOME%\libs\python3%py_version%.lib /OUT:exiv2api.pyd del exiv2api.exp exiv2api.obj exiv2api.lib ``` -------------------------------- ### Handling Encoding for Image Paths Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Shows how to handle non-ASCII characters in file paths by specifying an encoding parameter when initializing the Image object. ```python from pyexiv2 import Image img = Image(path, encoding='utf-8') img = Image(path, encoding='GBK') img = Image(path, encoding='ISO-8859-1') ``` -------------------------------- ### Reading IPTC Metadata Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Demonstrates reading IPTC metadata, highlighting how repeatable tags like Keywords are automatically parsed into Python lists. ```python import pyexiv2 with pyexiv2.Image('./photo.jpg') as img: iptc = img.read_iptc() print(iptc.get('Iptc.Application2.ObjectName')) print(iptc.get('Iptc.Application2.Keywords')) iptc_detail = img.read_iptc_detail() ``` -------------------------------- ### Copy Metadata Between Images Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Shows the recommended approach for copying metadata between images using copy_to_another_image, which is more efficient than individual modify calls. ```python with pyexiv2.Image(r'.\pyexiv2\tests\1.jpg') as img1: with pyexiv2.Image(r'.\pyexiv2\tests\2.jpg') as img2: img1.copy_to_another_image(img2, exif=True, iptc=True, xmp=True, comment=False, icc=False, thumbnail=False) ``` -------------------------------- ### pyexiv2 Utility Functions API Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Provides utility functions for namespace registration, log level setting, and conversion between EXIF, IPTC, and XMP metadata formats. ```python def registerNs(namespace: str, prefix: str) def set_log_level(level=2) def convert_exif_to_xmp(data: dict, encoding='utf-8') -> dict def convert_iptc_to_xmp(data: dict, encoding='utf-8') -> dict def convert_xmp_to_exif(data: dict, encoding='utf-8') -> dict def convert_xmp_to_iptc(data: dict, encoding='utf-8') -> dict ``` -------------------------------- ### Reading XMP Metadata Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Covers reading XMP metadata, including handling multi-value tags as lists, LangAlt tags as dictionaries, and retrieving raw XML strings. ```python import pyexiv2 with pyexiv2.Image('./photo.jpg') as img: xmp = img.read_xmp() print(xmp.get('Xmp.dc.subject')) print(xmp.get('Xmp.dc.title')) raw_xmp = img.read_raw_xmp() print(raw_xmp) ``` -------------------------------- ### Image Management and Metadata Access Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Methods for initializing an Image object, managing file resources, and performing read/write operations on metadata tags. ```APIDOC ## Image Class Methods ### Description The Image class allows opening image files to access and modify their embedded metadata (EXIF, IPTC, XMP). It supports Unicode paths and custom character encodings. ### Methods - **Image(path, encoding='utf-8')**: Opens an image file. Use 'with' statement for automatic closure. - **close()**: Releases file resources and memory. - **read_exif() / read_iptc() / read_xmp()**: Returns a dictionary of current metadata tags. - **modify_exif(dict) / modify_iptc(dict) / modify_xmp(dict)**: Updates metadata tags. Assigning None to a key deletes the tag. - **clear_exif() / clear_iptc() / clear_xmp()**: Removes all metadata of the specified type. ### Request Example ```python # Reading metadata with pyexiv2.Image('photo.jpg') as img: exif_data = img.read_exif() # Modifying metadata img = pyexiv2.Image('photo.jpg') img.modify_xmp({'Xmp.xmp.Rating': '5', 'Xmp.xmp.CreateDate': None}) img.close() ``` ### Response #### Success Response (Dict) - **metadata** (dict) - A dictionary mapping tag names (keys) to their string values. ``` -------------------------------- ### Process Image Data from Bytes Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Demonstrates opening images directly from byte streams using the ImageData class, including reading and modifying metadata and saving the updated bytes back to a file. ```python # Reading metadata from bytes with open(r'.\pyexiv2\tests\1.jpg', 'rb') as f: with pyexiv2.ImageData(f.read()) as img: data = img.read_exif() # Modifying metadata and saving bytes with open(r'.\pyexiv2\tests\1.jpg', 'rb+') as f: with pyexiv2.ImageData(f.read()) as img: changes = {'Iptc.Application2.ObjectName': 'test'} img.modify_iptc(changes) f.seek(0) f.truncate() f.write(img.get_bytes()) ``` -------------------------------- ### Metadata Conversion Utilities Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Utility functions for converting between different metadata standards like EXIF, IPTC, and XMP. ```APIDOC ## Metadata Conversion Functions ### Description These functions allow for mapping metadata fields between different standards (e.g., EXIF to XMP). ### Functions - **convert_exif_to_xmp(data: dict, encoding='utf-8') -> dict** - **convert_iptc_to_xmp(data: dict, encoding='utf-8') -> dict** - **convert_xmp_to_exif(data: dict, encoding='utf-8') -> dict** - **convert_xmp_to_iptc(data: dict, encoding='utf-8') -> dict** ### Response Example ```python # Returns a dictionary formatted for the target metadata standard converted_data = pyexiv2.convert_exif_to_xmp(exif_dict) ``` ``` -------------------------------- ### Clear Image Metadata with pyexiv2 Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Demonstrates the clear_* methods to remove specific metadata segments like EXIF, IPTC, XMP, comments, ICC profiles, and thumbnails. ```python import pyexiv2 with pyexiv2.Image('./photo.jpg') as img: img.clear_exif() img.clear_iptc() img.clear_xmp() img.clear_comment() img.clear_icc() img.clear_thumbnail() print(img.read_exif()) ``` -------------------------------- ### Manage ICC Profiles with pyexiv2 Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Shows how to read, replace, and clear ICC color profiles embedded within an image file. ```python import pyexiv2 with pyexiv2.Image('./photo.jpg') as img: icc_data = img.read_icc() print(f'ICC profile size: {len(icc_data)} bytes') with open('./sRGB.icc', 'rb') as f: new_icc = f.read() img.modify_icc(new_icc) img.clear_icc() ``` -------------------------------- ### Modify XMP Metadata with pyexiv2 Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Explains updating XMP tags using modify_xmp and setting raw XML content with modify_raw_xmp. Multi-value tags are supported via lists. ```python import pyexiv2 with pyexiv2.Image('./photo.jpg') as img: changes = { 'Xmp.xmp.CreateDate': '2024-01-15T10:30:00.000', 'Xmp.xmp.Rating': '5', 'Xmp.dc.description': 'A beautiful sunset', 'Xmp.dc.subject': ['photography', 'nature', 'sunset'], 'Xmp.xmp.Label': None } img.modify_xmp(changes) xmp = img.read_xmp() print(xmp['Xmp.dc.subject']) with pyexiv2.Image('./photo.jpg') as img: raw_xmp = '\n \n \n \n John Doe\n \n \n ' img.modify_raw_xmp(raw_xmp) ``` -------------------------------- ### Modifying Metadata Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Demonstrates how to update or delete metadata tags using modify methods. It also covers registering custom namespaces for XMP tags. ```python # Modifying and deleting tags dict1 = {'Xmp.xmp.CreateDate': '2019-06-23T19:45:17.834', 'Xmp.xmp.Rating': None} img.modify_xmp(dict1) # Registering a namespace pyexiv2.registerNs('a namespace for test', 'Ns1') img.modify_xmp({'Xmp.Ns1.mytag1': 'Hello'}) ``` -------------------------------- ### pyexiv2 Version Information Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Constants defining the versions of the pyexiv2 library and its underlying Exiv2 C++ library. ```python __version__ = '2.15.5' __exiv2_version__ = '0.28.7' ``` -------------------------------- ### Image Metadata Operations Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Methods for reading and writing EXIF, IPTC, and XMP metadata from image files or byte streams. ```APIDOC ## Image Metadata Access ### Description Provides methods to read and modify metadata tags from images. Supports EXIF, IPTC, and XMP formats. ### Methods - `read_exif()` / `read_exif_detail()`: Retrieves EXIF tags as a dictionary or detailed object. - `read_iptc()` / `read_iptc_detail()`: Retrieves IPTC tags, handling repeatable tags as lists. - `read_xmp()`: Retrieves XMP tags, handling multi-value tags and language alternatives. - `modify_exif(data)`, `modify_iptc(data)`, `modify_xmp(data)`: Updates metadata tags. ### Usage Example ```python import pyexiv2 # Opening an image with pyexiv2.Image('./photo.jpg') as img: # Reading EXIF exif = img.read_exif() # Reading IPTC iptc = img.read_iptc() # Reading XMP xmp = img.read_xmp() ``` ### Response - **Success**: Returns a dictionary mapping tag names to their respective values (or lists/dicts for complex types). ``` -------------------------------- ### Processing Image Data from Memory Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Shows how to use the ImageData class to manipulate metadata directly from byte streams. This is useful for processing images without relying on file system paths. ```python import pyexiv2 # Reading metadata from bytes with open('./photo.jpg', 'rb') as f: with pyexiv2.ImageData(f.read()) as img: exif = img.read_exif() xmp = img.read_xmp() # Modifying metadata and saving back to file with open('./photo.jpg', 'rb+') as f: with pyexiv2.ImageData(f.read()) as img: img.modify_iptc({'Iptc.Application2.ObjectName': 'My Photo'}) f.seek(0) f.truncate() f.write(img.get_bytes()) ``` -------------------------------- ### Metadata Conversion API Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Utility functions to transform metadata between different standards like EXIF, IPTC, and XMP. ```APIDOC ## [METHOD] convert_exif_to_xmp / convert_xmp_to_exif ### Description Converts metadata dictionaries between different formats to ensure compatibility across various image standards. ### Method Python Function Call ### Parameters #### Request Body - **data** (dict) - Required - The metadata dictionary to convert. ### Response #### Success Response (200) - **result** (dict) - The converted metadata dictionary. ``` -------------------------------- ### Image Metadata Management API Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md The Image class provides methods to load image files and perform read/modify/clear operations on metadata such as EXIF, IPTC, and XMP. ```APIDOC ## CLASS Image ### Description Represents an image file and provides methods to access and manipulate its embedded metadata. ### Methods - **__init__(filename, encoding='utf-8')** - Initializes the Image object with a file path. - **read_exif(encoding='utf-8') -> dict** - Returns a dictionary of EXIF metadata. - **modify_exif(data: dict, encoding='utf-8')** - Updates the image EXIF metadata with the provided dictionary. - **clear_exif()** - Removes all EXIF metadata from the image. - **read_thumbnail() -> bytes** - Returns the image thumbnail as bytes. - **modify_thumbnail(data: bytes)** - Sets the image thumbnail data. ### Request Example ```python import pyexiv2 img = pyexiv2.Image('photo.jpg') exif_data = img.read_exif() img.modify_exif({'Exif.Image.Artist': 'Photographer Name'}) img.close() ``` ``` -------------------------------- ### Metadata Copying API Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Efficiently copies metadata between image files with selective control over metadata types. ```APIDOC ## [METHOD] copy_to_another_image ### Description Copies metadata from a source image to a destination image. Supports selective copying of EXIF, IPTC, XMP, comments, ICC profiles, and thumbnails. ### Method Python Method Call ### Parameters #### Request Body - **destination** (Image object) - Required - The target image instance. - **exif** (bool) - Optional - Copy EXIF data. - **iptc** (bool) - Optional - Copy IPTC data. - **xmp** (bool) - Optional - Copy XMP data. ### Response #### Success Response (200) - **status** (void) - Returns nothing upon successful copy. ``` -------------------------------- ### pyexiv2 Image Class API Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Defines the Image class for pyexiv2, including methods for initializing, closing, reading, modifying, clearing, and copying image metadata (EXIF, IPTC, XMP, etc.). ```python class Image: def __init__(self, filename, encoding='utf-8') def close(self) def get_pixel_width (self) -> int def get_pixel_height(self) -> int def get_mime_type (self) -> str def get_access_mode (self) -> dict def read_exif (self, encoding='utf-8') -> dict def read_exif_detail(self, encoding='utf-8') -> dict def read_iptc (self, encoding='utf-8') -> dict def read_iptc_detail(self, encoding='utf-8') -> dict def read_xmp (self, encoding='utf-8') -> dict def read_xmp_detail (self, encoding='utf-8') -> dict def read_raw_xmp (self, encoding='utf-8') -> str def read_comment (self, encoding='utf-8') -> str def read_icc (self) -> bytes def read_thumbnail (self) -> bytes def modify_exif (self, data: dict, encoding='utf-8') def modify_iptc (self, data: dict, encoding='utf-8') def modify_xmp (self, data: dict, encoding='utf-8') def modify_raw_xmp (self, data: str, encoding='utf-8') def modify_comment (self, data: str, encoding='utf-8') def modify_icc (self, data: bytes) def modify_thumbnail(self, data: bytes) def clear_exif (self) def clear_iptc (self) def clear_xmp (self) def clear_comment (self) def clear_icc (self) def clear_thumbnail () def copy_to_another_image(self, another_image, exif=True, iptc=True, xmp=True, comment=True, icc=True, thumbnail=True) ``` -------------------------------- ### Manage EXIF Thumbnails in JPEG Images with pyexiv2 Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Demonstrates how to read, modify, and clear EXIF thumbnails embedded in JPEG images using pyexiv2. Only JPEG thumbnails are supported for insertion. This involves reading thumbnail data into bytes, writing it to a file, or providing new JPEG data to modify or clear the existing thumbnail. ```python import pyexiv2 with pyexiv2.Image('./photo.jpg') as img: # Read existing thumbnail thumb_data = img.read_thumbnail() if thumb_data: with open('./thumbnail.jpg', 'wb') as f: f.write(thumb_data) # Set a new thumbnail (must be JPEG format) with open('./new_thumbnail.jpg', 'rb') as f: new_thumb = f.read() img.modify_thumbnail(new_thumb) # Clear thumbnail img.clear_thumbnail() ``` -------------------------------- ### Manage JPEG Comments with pyexiv2 Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Covers reading, modifying, and clearing the JPEG COM segment, which is distinct from standard metadata formats. ```python import pyexiv2 with pyexiv2.Image('./photo.jpg') as img: img.modify_comment('Hello World!\nThis is a test comment.\n') comment = img.read_comment() print(comment) img.clear_comment() ``` -------------------------------- ### Troubleshoot GLIBC version error on Linux Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md This Python code snippet demonstrates an OSError that can occur on Linux if the GLIBC version is too old. It suggests upgrading GLIBC or the Linux distribution. ```python import pyexiv2 # Traceback indicates a GLIBC_2.32 not found error. ``` -------------------------------- ### Modify EXIF Metadata with pyexiv2 Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Demonstrates how to update or delete EXIF tags using the modify_exif method. It accepts a dictionary of key-value pairs where setting a value to None removes the tag. ```python import pyexiv2 with pyexiv2.Image('./photo.jpg') as img: changes = { 'Exif.Image.ImageDescription': 'Beautiful sunset over mountains', 'Exif.Image.Artist': 'Jane Photographer', 'Exif.Image.Copyright': 'Copyright 2024', 'Exif.Image.Rating': '5', 'Exif.Image.Software': None } img.modify_exif(changes) exif = img.read_exif() print(exif.get('Exif.Image.ImageDescription')) print(exif.get('Exif.Image.Software')) ``` -------------------------------- ### Manage JPEG Comment Segments Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Demonstrates how to read, modify, and clear the JPEG COM segment. Note that this functionality is specific to JPEG images and may raise errors for other formats. ```python img.modify_comment('Hello World! \n你好!\n') img.read_comment() img.clear_comment() img.read_comment() # Handling unsupported formats img = pyexiv2.Image('2.gif') img.modify_comment('Hello World!') # Raises RuntimeError ``` -------------------------------- ### pyexiv2 ImageData Class API Source: https://github.com/leohsiao1/pyexiv2/blob/master/docs/Tutorial.md Extends the Image class to handle image data directly from bytes. Provides a method to retrieve the image data as bytes. ```python class ImageData(Image): def __init__(self, data: bytes) def get_bytes(self) -> bytes ``` -------------------------------- ### Modify IPTC Metadata with pyexiv2 Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Shows how to update IPTC tags using the modify_iptc method. Repeatable tags like Keywords are handled by passing a list of values. ```python import pyexiv2 with pyexiv2.Image('./photo.jpg') as img: changes = { 'Iptc.Application2.ObjectName': 'Sunset Photography', 'Iptc.Application2.Copyright': 'Copyright 2024 Jane Doe', 'Iptc.Application2.Keywords': ['sunset', 'mountains', 'landscape', 'nature'], 'Iptc.Application2.City': 'Denver', 'Iptc.Application2.ProvinceState': 'Colorado', 'Iptc.Application2.CountryName': 'United States' } img.modify_iptc(changes) iptc = img.read_iptc() print(iptc['Iptc.Application2.Keywords']) ``` -------------------------------- ### ImageData Memory Operations Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Operations for processing image data directly from memory using the ImageData class. ```APIDOC ## ImageData Class ### Description Allows processing of image bytes without requiring a physical file path, useful for streams or in-memory processing. ### Usage - `ImageData(bytes)`: Initializes the object with raw image data. - `get_bytes()`: Returns the modified image data as bytes. ### Example ```python with open('./photo.jpg', 'rb') as f: with pyexiv2.ImageData(f.read()) as img: img.modify_iptc({'Iptc.Application2.ObjectName': 'New Title'}) modified_data = img.get_bytes() ``` ``` -------------------------------- ### Thumbnail Management API Source: https://context7.com/leohsiao1/pyexiv2/llms.txt Methods for reading, modifying, and clearing embedded JPEG thumbnails in images. ```APIDOC ## [METHOD] read_thumbnail / modify_thumbnail / clear_thumbnail ### Description Manages EXIF thumbnails embedded in JPEG images. Note that only JPEG format thumbnails are supported for insertion. ### Method Python Method Calls ### Parameters #### Request Body - **thumbnail_data** (bytes) - Required - The binary JPEG data for the thumbnail. ### Response #### Success Response (200) - **data** (bytes) - The binary thumbnail data retrieved from the image. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.