Skip to content

Commit

Permalink
[update] support old version for quality
Browse files Browse the repository at this point in the history
  • Loading branch information
NatLee committed Oct 11, 2023
1 parent d36846f commit 677fa3e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/heic2png/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.1.2"
__version__ = "1.1.3"

from .heic2png import *
from .cli import main
4 changes: 2 additions & 2 deletions src/heic2png/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def cli(args):
if args.output_path:
print(f'Specified output path: `{args.output_path}`')

if not 1 <= args.quality <= 100:
if args.quality and not 1 <= args.quality <= 100:
print('Error: Quality should be a value between 1 and 100.')
return

Expand Down Expand Up @@ -60,7 +60,7 @@ def main():
parser = argparse.ArgumentParser(description="Convert HEIC images to PNG.")
parser.add_argument("-i", "--input_path", required=True, help="Path to the input HEIC image.")
parser.add_argument("-o", "--output_path", help="Path to save the converted PNG image.")
parser.add_argument("-q", "--quality", type=int, default=95, help="Quality of the converted PNG image (1-100).")
parser.add_argument("-q", "--quality", type=int, help="Quality of the converted PNG image (1-100).")
parser.add_argument("-w", "--overwrite", action="store_true", help="Overwrite the existing file if it already exists.")

args = parser.parse_args()
Expand Down
25 changes: 14 additions & 11 deletions src/heic2png/heic2png.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import subprocess
from pathlib import Path
from typing import Optional
from PIL import Image
from pillow_heif import register_heif_opener

register_heif_opener()

class HEIC2PNG:
def __init__(self, image_file_path: str, quality: int = 95, overwrite: bool = False):
def __init__(self, image_file_path: str, quality: Optional[int] = None, overwrite: bool = False):
"""
Initializes the HEIC2PNG converter.
:param image_file_path: Path to the HEIC image file.
:param quality: Quality of the converted PNG image (1-100).
:param overwrite: Whether to overwrite the file if it already exists.
"""
self.image_file_path = Path(image_file_path)
self.quality = quality
self.overwrite = overwrite
self.image_file_path: Path = Path(image_file_path)
self.quality: Optional[int] = quality
self.overwrite: bool = overwrite

if not self.image_file_path.is_file():
raise FileNotFoundError(f"The file {image_file_path} does not exist.")

if self.image_file_path.suffix.lower() != '.heic':
raise ValueError("The provided file is not a HEIC image.")

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

def save(self, output_image_file_path=None, extension='.png') -> Path:
def save(self, output_image_file_path: Optional[str] = None, extension: str = '.png') -> Path:
"""
Converts and saves the HEIC image to PNG format.
Expand All @@ -34,19 +36,20 @@ def save(self, output_image_file_path=None, extension='.png') -> Path:
:return: Path where the converted image is saved.
"""
if output_image_file_path:
output_path = Path(output_image_file_path)
output_path: Path = Path(output_image_file_path)
if output_path.suffix.lower() != extension:
raise ValueError("The output file extension does not match the specified extension.")
else:
output_path = self.image_file_path.with_suffix(extension)
output_path: Path = self.image_file_path.with_suffix(extension)

if not self.overwrite and output_path.exists():
raise FileExistsError(f"The file {output_path} already exists.")

self.image.save(output_path)

# Optimize PNG with pngquant
quality_str = f'{self.quality}-{self.quality}'
subprocess.run(['pngquant', '--quality', quality_str, '-f', '-o', str(output_path), str(output_path)])
# Optimize PNG with pngquant if quality is specified
if self.quality:
quality_str: str = f'{self.quality}-{self.quality}'
subprocess.run(['pngquant', '--quality', quality_str, '-f', '-o', str(output_path), str(output_path)])

return output_path

0 comments on commit 677fa3e

Please sign in to comment.