# Image Metadata Extractor

This utility extracts EXIF metadata from images using the `Pillow` library. It decodes standard EXIF tags (like Camera Model, DateTime, ISO) into human-readable format.

**Modules Used:**
*   [[programming/python/modules/pillow-module|Pillow]]: To open images and access EXIF data.
*   [[programming/python/modules/argparse-module|argparse]]: To handle command-line arguments.

## Installation

```bash
pip install Pillow
```

## The Code

Save this as `metadata.py`.

```python
from PIL import Image, ExifTags
import argparse
import sys

def extract_metadata(image_path):
    try:
        img = Image.open(image_path)
        
        # getexif() returns a dictionary of tag IDs and values
        exif_data = img.getexif()
        
        if not exif_data:
            print(f"No EXIF metadata found in {image_path}")
            return

        print(f"Metadata for {image_path}:")
        print("-" * 40)

        for tag_id in exif_data:
            # Get the human-readable tag name from the ID
            tag = ExifTags.TAGS.get(tag_id, tag_id)
            data = exif_data.get(tag_id)

            # Decode bytes to string if possible for cleaner output
            if isinstance(data, bytes):
                try:
                    data = data.decode()
                except:
                    pass # Keep as bytes if decode fails

            print(f"{tag:25}: {data}")

    except IOError:
        print(f"Error: Could not open image file {image_path}")
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Image Metadata Extractor")
    parser.add_argument("image", help="Path to the image file")
    args = parser.parse_args()
    
    extract_metadata(args.image)
```

## Usage

```bash
python metadata.py photo.jpg
```

[[programming/python/python]]