diff --git a/image_processing/image_processing.py b/image_processing/image_processing.py index dca0e03..2b24bdf 100644 --- a/image_processing/image_processing.py +++ b/image_processing/image_processing.py @@ -8,14 +8,14 @@ if TYPE_CHECKING: from typing import Callable, Union - TStrorPath = Union[str, Path] + TStrOrPath = Union[str, Path] DEFAULT_FORMAT = "jpeg" class ImageProcessing: - def __init__(self, source: "TStrorPath" = "", *, temp_folder: "TStrorPath" = ""): + def __init__(self, source: "TStrOrPath" = "", *, temp_folder: "TStrOrPath" = ""): self._processor = VipsProcessor() self._source: str = str(source) self._loader: dict = {} @@ -45,7 +45,7 @@ def operation(*args, **kw) -> "ImageProcessing": return operation - def source(self, path: "TStrorPath") -> "ImageProcessing": + def source(self, path: "TStrOrPath") -> "ImageProcessing": """ """ copy = self._copy() copy._source = str(path) @@ -80,7 +80,7 @@ def convert(self, format: str) -> "ImageProcessing": copy._format = format return copy - def save(self, destination: "TStrorPath" = "", save: bool = True) -> str: + def save(self, destination: "TStrOrPath" = "", save: bool = True) -> str: """ Run the defined processing and get the result. Allows specifying the source file and destination. @@ -101,6 +101,15 @@ def save(self, destination: "TStrorPath" = "", save: bool = True) -> str: save=save, ) + def get_temp_filename(self, destination: "TStrOrPath" = "") -> str: + """Return a filename that, for the same source path, options, + operations (in the same order), etc., will be the same. + """ + ops = str(self.options).encode("utf8", errors="ignore") + hash = md5(ops).hexdigest() + format = self._get_destination_format(destination) + return f"{hash}.{format}" + # Private def _copy(self) -> "ImageProcessing": @@ -112,7 +121,7 @@ def _copy(self) -> "ImageProcessing": copy._operations = self._operations[:] return copy - def _get_destination_format(self, destination: "TStrorPath") -> str: + def _get_destination_format(self, destination: "TStrOrPath") -> str: format = "" if destination: format = self._get_format(destination) @@ -123,19 +132,18 @@ def _get_destination_format(self, destination: "TStrorPath") -> str: or DEFAULT_FORMAT ) - def _get_destination(self, destination: "TStrorPath", format: str) -> str: - destination = Path(destination) if destination else self._get_temp_destination() + def _get_destination(self, destination: "TStrOrPath", format: str) -> str: + if destination: + destination = Path(destination) + else: + destination = self._get_temp_destination() return str(destination.with_suffix(f".{format}")) def _get_temp_destination(self) -> "Path": - filename = self._get_temp_filename() + filename = self.get_temp_filename() if not self._temp_folder: self._temp_folder = Path(tempfile.mkdtemp()) return self._temp_folder / filename - def _get_temp_filename(self) -> str: - ops = str(self.options).encode("utf8", errors="ignore") - return md5(ops).hexdigest() - - def _get_format(self, file_path: "TStrorPath") -> str: + def _get_format(self, file_path: "TStrOrPath") -> str: return Path(file_path).suffix.lstrip(".") diff --git a/setup.cfg b/setup.cfg index 05c20f3..c483eef 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = image-processing-egg -version = 0.2 +version = 0.3 url = https://github.com/jpsca/image-processing-egg project_urls = Issue tracker = https://github.com/jpsca/image-processing-egg/issues