Skip to content

Commit

Permalink
Restructure package and expand usage options (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefmolin authored Oct 7, 2024
1 parent 7b2905b commit 32f7b84
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 47 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
49 changes: 48 additions & 1 deletion src/exif_stripper/__init__.py
Original file line number Diff line number Diff line change
@@ -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
46 changes: 1 addition & 45 deletions src/exif_stripper/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 32f7b84

Please sign in to comment.