Skip to content

Commit

Permalink
Merge pull request #11 from TimNekk/develop (v1.3.0)
Browse files Browse the repository at this point in the history
Added output_format option
  • Loading branch information
TimNekk authored Apr 9, 2023
2 parents 11d4432 + 4964e42 commit a0e7e9b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pip install -U gigapixel
2. Use `.process()` method to enhance image

```python
from gigapixel import Gigapixel, Scale, Mode
from gigapixel import Gigapixel, Scale, Mode, OutputFormat
from pathlib import Path

# Path to Gigapixel executable file.
Expand All @@ -55,7 +55,7 @@ app = Gigapixel(exe_path, output_suffix)

# Process image.
image = Path('path/to/image.jpg')
output_path = app.process(image, scale=Scale.X2, mode=Mode.STANDARD, delete_from_history=True)
output_path = app.process(image, scale=Scale.X2, mode=Mode.STANDARD, delete_from_history=True, output_format=OutputFormat.PNG)

# Print output path.
print(output_path)
Expand Down
2 changes: 1 addition & 1 deletion gigapixel/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .gigapixel import Gigapixel, Mode, Scale
from .gigapixel import Gigapixel, Mode, Scale, OutputFormat
from .exceptions import NotFile, FileAlreadyExists, GigapixelException, ElementNotFound
65 changes: 55 additions & 10 deletions gigapixel/gigapixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class Mode(Enum):
VERY_COMPRESSED = "Very Compressed"


class OutputFormat(Enum):
PRESERVE_SOURCE_FORMAT = "Preserve Source Format"
JPG = "JPG"
JPEG = "JPEG"
TIF = "TIF"
TIFF = "TIFF"
PNG = "PNG"
DNG = "DNG"


class Gigapixel:
def __init__(self,
executable_path: Path,
Expand Down Expand Up @@ -56,8 +66,10 @@ def open_photo(self, photo_path: Path) -> None:
send_keys('^v {ENTER}')

@log("Saving photo", "Photo saved", level=Level.DEBUG)
def save_photo(self) -> None:
send_keys('^S {ENTER}')
def save_photo(self, output_format: OutputFormat) -> None:
send_keys('^S')
self._set_output_format(output_format)
send_keys('{ENTER}')
self._main_window.child_window(title="Cancel Processing", control_type="Button").wait_not('visible',
timeout=60)

Expand All @@ -72,6 +84,31 @@ def set_processing_options(self, scale: Optional[Scale] = None, mode: Optional[M
if mode:
self._set_mode(mode)

def _set_output_format(self, save_format: OutputFormat) -> None:
self._main_window.ComboBox.click_input()

if save_format == OutputFormat.PRESERVE_SOURCE_FORMAT:
self._main_window.ListItem.click_input()
send_keys('{TAB}')
elif save_format == OutputFormat.JPG:
self._main_window.ListItem2.click_input()
send_keys('{TAB}')
elif save_format == OutputFormat.JPEG:
self._main_window.ListItem3.click_input()
send_keys('{TAB}')
elif save_format == OutputFormat.TIF:
self._main_window.ListItem4.click_input()
send_keys('{TAB} {TAB} {TAB}')
elif save_format == OutputFormat.TIFF:
self._main_window.ListItem5.click_input()
send_keys('{TAB} {TAB} {TAB}')
elif save_format == OutputFormat.PNG:
self._main_window.ListItem6.click_input()
send_keys('{TAB}')
elif save_format == OutputFormat.DNG:
self._main_window.ListItem7.click_input()
send_keys('{TAB}')

def _set_scale(self, scale: Scale):
if self.scale == scale:
return
Expand Down Expand Up @@ -114,11 +151,11 @@ def _open_topaz(self) -> Application:
return instance

@log("Checking path: {}", "Path is valid", format=(1,), level=Level.DEBUG)
def _check_path(self, path: Path) -> None:
def _check_path(self, path: Path, output_format: OutputFormat) -> None:
if not path.is_file():
raise NotFile(f"Path is not a file: {path}")

save_path = self._get_save_path(path)
save_path = self._get_save_path(path, output_format)
if save_path.name in os.listdir(path.parent):
raise FileAlreadyExists(f"Output file already exists: {save_path}")

Expand All @@ -128,18 +165,26 @@ def _remove_suffix(input_string: str, suffix: str) -> str:
return input_string[:-len(suffix)]
return input_string

def _get_save_path(self, path: Path) -> Path:
return path.parent / (Gigapixel._remove_suffix(path.name, path.suffix) + self._output_suffix + path.suffix)
def _get_save_path(self, path: Path, output_format: OutputFormat) -> Path:
extension = path.suffix if output_format == OutputFormat.PRESERVE_SOURCE_FORMAT else f".{output_format.value.lower()}"
return path.parent / (Gigapixel._remove_suffix(path.name, path.suffix) + self._output_suffix + extension)

@log(start="Starting processing: {}", format=(1,))
@log(end="Finished processing: {}", format=(1,), level=Level.SUCCESS)
def process(self, photo_path: Path, scale: Scale = Scale.X2, mode: Mode = Mode.STANDARD, delete_from_history: bool = True) -> Path:
self._check_path(photo_path)
def process(self,
photo_path: Path,
scale: Scale = Scale.X2,
mode: Mode = Mode.STANDARD,
delete_from_history: bool = True,
output_format: OutputFormat = OutputFormat.PRESERVE_SOURCE_FORMAT
) -> Path:
self._check_path(photo_path, output_format)

self._app.open_photo(photo_path)
self._app.set_processing_options(scale, mode)
self._app.save_photo()
self._app.save_photo(output_format)

if delete_from_history:
self._app.delete_photo()

return self._get_save_path(photo_path)
return self._get_save_path(photo_path, output_format)

0 comments on commit a0e7e9b

Please sign in to comment.