Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure package and expand usage options #46

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading