Skip to content

Commit

Permalink
[update] add simple cli
Browse files Browse the repository at this point in the history
  • Loading branch information
NatLee committed Sep 1, 2022
1 parent ce044b9 commit 1da03ed
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# HEIC2PNG

[![Test](https://github.com/NatLee/HEIC2PNG/actions/workflows/test.yml/badge.svg)](https://github.com/NatLee/HEIC2PNG/actions/workflows/test.yml)[![Release](https://github.com/NatLee/HEIC2PNG/actions/workflows/release.yml/badge.svg)](https://github.com/NatLee/HEIC2PNG/actions/workflows/release.yml)

This is a tool for converting format of HEIC image to PNG by using Python.

And, I used the pypi package template to generate this repo, you can check it in the [references](#References).
And it can be used with python simple CLI.

I used the pypi package template to generate this repo, you can check it in the [references](#References).

## Installation

Expand All @@ -12,6 +16,8 @@ pip install heic2png

## Usage

Common use with code below.

```python
from heic2png import HEIC2PNG

Expand All @@ -21,6 +27,20 @@ if __name__ == '__main__':

```

And, you can try simple CLI with this.

```bash
python -m heic2png --input_path test.heic --output_path test.png
```

Or, you want to keep original name, just use this.

```bash
python -m heic2png --input_path test.heic
```

It'll generate `test.png` for you.

## References

- [Example PyPi Package](https://github.com/tomchen/example_pypi_package)
Expand Down
10 changes: 2 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,9 @@
'Operating System :: OS Independent',
],
python_requires='>=3.7',
install_requires=['pillow', 'pillow-heif'],
install_requires=['pillow', 'pillow-heif', 'fire'],
extras_require={
'dev': ['check-manifest'],
# 'test': ['coverage'],
},
# entry_points={
# 'console_scripts': [ # This can provide executable scripts
# 'run=examplepy:main',
# You can execute `run` in bash to run `main()` in src/examplepy/__init__.py
# ],
# },
}
)
2 changes: 1 addition & 1 deletion src/heic2png/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "0.9.0"
__version__ = "0.9.5"

from .heic2png import *
25 changes: 25 additions & 0 deletions src/heic2png/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from fire import Fire
from pillow_heif import register_heif_opener

from .heic2png import HEIC2PNG

def main(input_path:str, output_path:str=None):
print(f'----- Input file path is `{input_path}`')
if output_path:
print(f'----- Set the output path is `{output_path}`')
try:
heic_img = HEIC2PNG(input_path)
print(f'----- Processing...')
output_path = heic_img.save(output_path)
print(f'----- Output file path is `{output_path}`')
except FileExistsError:
print('----- File has been already existed!')
except ValueError:
print('----- You need to check the format of image!')
print('Input must be `.heic` and output must be `.png`.')
except Exception as e:
print(f'----- Error with {e}')
print('----- Please report this issue!')

register_heif_opener()
Fire(main)
10 changes: 7 additions & 3 deletions src/heic2png/heic2png.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
from pathlib import Path

from PIL import Image

from pillow_heif import register_heif_opener
register_heif_opener()

class HEIC2PNG:
def __init__(self, image_file_path:str):
self.__opener = register_heif_opener()
self.image_file_path = Path(image_file_path)
if self.image_file_path.suffix.lower() != '.heic':
raise ValueError

self.image = Image.open(self.image_file_path)

def save(self, output_image_file_path=None, extension='.png'):
def save(self, output_image_file_path=None, extension='.png') -> Path:
if output_image_file_path:
output_path = Path(output_image_file_path)
if output_path.suffix.lower() != extension:
raise ValueError
else:
output_path = self.image_file_path.with_name(f"{self.image_file_path.stem}{extension}")
if output_path.exists():
raise FileExistsError
self.image.save(output_image_file_path)
self.image.save(output_path)
return output_path
3 changes: 1 addition & 2 deletions tests/test_heic2png.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@

from heic2png.heic2png import HEIC2PNG


class TestHEIC2PNG(unittest.TestCase):

__test_input_filename = './test.heic'
__test_output_filename = './test.png'
register_heif_opener()

def test_save(self):

Expand All @@ -26,6 +24,7 @@ def test_save(self):
@pytest.fixture(autouse=True)
def run_around_tests(self):
# before
register_heif_opener()
yield
# after
input_file = Path(self.__test_input_filename)
Expand Down

0 comments on commit 1da03ed

Please sign in to comment.