diff --git a/README.md b/README.md index efa6917..97cb4db 100644 --- a/README.md +++ b/README.md @@ -46,16 +46,52 @@ Pre-commit hook to ensure image metadata (EXIF data and extended attributes) is ## Usage +### Pre-commit hook + Add the following to your `.pre-commit-config.yaml` file: ```yaml - repo: https://github.com/stefmolin/exif-stripper - rev: 0.3.1 + rev: 0.4.0 hooks: - id: strip-exif ``` Be sure to check out the [pre-commit documentation](https://pre-commit.com/#pre-commit-configyaml---hooks) for additional configuration options. +### Command line + +First, install the `exif-stripper` package from PyPI: + +```shell +$ python -m pip install exif-stripper +``` + +Then, use the `strip-exif` entry point on the file(s) of your choice: + +```shell +$ strip-exif /path/to/file [/path/to/another/file] +``` + +Run `strip-exif --help` for more information. + +### Python + +First, install the `exif-stripper` package from PyPI: + +```shell +$ python -m pip install exif-stripper +``` + +Then, use the `process_image()` function on individual files (returns `True` if the file was altered and `False` otherwise): + +```python +from exif_stripper import process_image +process_image('/path/to/image') +``` + +*Note: This requires version 0.4.0 or above.* + ## Contributing + Please consult the [contributing guidelines](CONTRIBUTING.md). diff --git a/src/exif_stripper/__init__.py b/src/exif_stripper/__init__.py index 9c27d2d..5eb91e7 100644 --- a/src/exif_stripper/__init__.py +++ b/src/exif_stripper/__init__.py @@ -1,3 +1,50 @@ """EXIF stripper.""" -__version__ = '0.3.1' +from __future__ import annotations + +import os +import platform + +from PIL import Image, UnidentifiedImageError + +__version__ = '0.4.0' + + +def process_image(filename: str | os.PathLike) -> bool: + """ + Process image metadata. + + Parameters + ---------- + filename : str | os.PathLike + The image file to check. + + Returns + ------- + bool + Indicator of whether metadata was stripped. + """ + has_changed = False + try: + # remove EXIF data + with Image.open(filename) as im: + if exif := im.getexif(): + exif.clear() + im.save(filename) + has_changed = True + except (FileNotFoundError, UnidentifiedImageError): + pass # not an image + else: + # remove extended attributes (Unix only) + if platform.system() != 'Windows': + from xattr import xattr + + xattr_obj = xattr(filename) + if xattr_obj.list(): + xattr_obj.clear() + has_changed = True + + if has_changed: + print(f'Stripped metadata from {filename}') + + return has_changed diff --git a/src/exif_stripper/cli.py b/src/exif_stripper/cli.py index 2cfc96b..380a706 100644 --- a/src/exif_stripper/cli.py +++ b/src/exif_stripper/cli.py @@ -3,57 +3,13 @@ from __future__ import annotations import argparse -import os -import platform from typing import Sequence -from PIL import Image, UnidentifiedImageError - -from . import __version__ +from . import __version__, process_image PROG = 'strip-exif' -def process_image(filename: str | os.PathLike) -> bool: - """ - Process image metadata. - - Parameters - ---------- - filename : str | os.PathLike - The image file to check. - - Returns - ------- - bool - Indicator of whether metadata was stripped. - """ - has_changed = False - try: - # remove EXIF data - with Image.open(filename) as im: - if exif := im.getexif(): - exif.clear() - im.save(filename) - has_changed = True - except (FileNotFoundError, UnidentifiedImageError): - pass # not an image - else: - # remove extended attributes (Unix only) - if platform.system() != 'Windows': - from xattr import xattr - - xattr_obj = xattr(filename) - if xattr_obj.list(): - xattr_obj.clear() - has_changed = True - - if has_changed: - print(f'Stripped metadata from {filename}') - - return has_changed - - def main(argv: Sequence[str] | None = None) -> int: """ Tool for stripping EXIF data from images.