Skip to content

Commit

Permalink
chore(concat): add download link and shorten docstring header
Browse files Browse the repository at this point in the history
  • Loading branch information
engeir committed Dec 12, 2023
1 parent 3d6c649 commit ec369be
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions cosmoplots/concat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Combine cosmoplots images in a subfigure layout."""
"""Combine images in a subfigure layout."""

# `Self` was introduced in 3.11, but returning the class type works from 3.7 onwards.
from __future__ import annotations
Expand All @@ -9,7 +9,7 @@


class Combine:
"""Combine images made with the cosmoplots mplstyle into a subfigure layout."""
"""Combine images into a subfigure layout."""

def __init__(self) -> None:
self._gravity = "northwest"
Expand Down Expand Up @@ -162,6 +162,7 @@ def _check_cli_available() -> None:
except subprocess.CalledProcessError as e:
raise ChildProcessError(
"Calling `convert --help` did not work. Are you sure you have imagemagick installed?"
" If not, resort to the ImageMagick website: https://imagemagick.org/script/download.php"
) from e

def _run_subprocess(self) -> None:
Expand All @@ -173,6 +174,7 @@ def _run_subprocess(self) -> None:
if self._w is None or self._h is None:
raise ValueError("You need to specify the files and grid first.")
idx = list(range(len(self._files)))
tmp_path = pathlib.Path(tmp_dir.name)
for i, file, label in zip(idx, self._files, self._labels):
# Add label to images
subprocess.call(
Expand All @@ -188,7 +190,7 @@ def _run_subprocess(self) -> None:
f"gravity {self._gravity} fill {self._color} text"
f" {self._pos[0]},{self._pos[1]} '{label}'"
),
pathlib.Path(tmp_dir.name) / f"{str(i)}.png",
tmp_path / f"{str(i)}.png",
]
)
# Create horizontal subfigures
Expand All @@ -197,17 +199,14 @@ def _run_subprocess(self) -> None:
idx_sub = idx[j * self._w : (j + 1) * self._w]
subprocess.call(
["convert", "+append"]
+ [pathlib.Path(tmp_dir.name) / f"{str(i)}.png" for i in idx_sub]
+ [pathlib.Path(tmp_dir.name) / f"subfigure_{j}.png"]
+ [tmp_path / f"{str(i)}.png" for i in idx_sub]
+ [tmp_path / f"subfigure_{j}.png"]
)

# Create vertical subfigures from horizontal subfigures
subprocess.call(
["convert", "-append"]
+ [
pathlib.Path(tmp_dir.name) / f"subfigure_{j}.png"
for j in range(self._h)
]
+ [tmp_path / f"subfigure_{j}.png" for j in range(self._h)]
+ [self._output.resolve()]
)

Expand Down

0 comments on commit ec369be

Please sign in to comment.