From 677fa3e3f7c298130f2d833f818a5f793a2c0db9 Mon Sep 17 00:00:00 2001 From: Nat Lee Date: Wed, 11 Oct 2023 15:51:05 +0800 Subject: [PATCH] [update] support old version for quality --- src/heic2png/__init__.py | 2 +- src/heic2png/cli.py | 4 ++-- src/heic2png/heic2png.py | 25 ++++++++++++++----------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/heic2png/__init__.py b/src/heic2png/__init__.py index a5d4fcd..cad41f3 100644 --- a/src/heic2png/__init__.py +++ b/src/heic2png/__init__.py @@ -1,4 +1,4 @@ -__version__ = "1.1.2" +__version__ = "1.1.3" from .heic2png import * from .cli import main diff --git a/src/heic2png/cli.py b/src/heic2png/cli.py index ed80ab1..3df57b2 100644 --- a/src/heic2png/cli.py +++ b/src/heic2png/cli.py @@ -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 @@ -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() diff --git a/src/heic2png/heic2png.py b/src/heic2png/heic2png.py index dd7544f..1a477c6 100644 --- a/src/heic2png/heic2png.py +++ b/src/heic2png/heic2png.py @@ -1,21 +1,23 @@ 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.") @@ -23,9 +25,9 @@ def __init__(self, image_file_path: str, quality: int = 95, overwrite: bool = Fa 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. @@ -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