Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grass.jupyter: move save() to BaseSeriesMap class to reduce redundancy #4378

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion python/grass/jupyter/baseseriesmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import grass.script as gs

from .map import Map
from .utils import get_number_of_cores
from .utils import get_number_of_cores, save_gif


class BaseSeriesMap:
Expand Down Expand Up @@ -210,3 +210,49 @@ def change_image(index):
width="100%", display="inline-flex", flex_flow="row wrap"
)
return widgets.HBox([play, slider, out_img], layout=layout)

def save(
self,
filename,
duration=500,
label=True,
font=None,
text_size=12,
text_color="gray",
):
"""
Creates a GIF animation of rendered layers.
Text color must be in a format accepted by PIL ImageColor module. For supported
formats, visit:
https://pillow.readthedocs.io/en/stable/reference/ImageColor.html#color-names
param str filename: name of output GIF file
param int duration: time to display each frame; milliseconds
param bool label: include label on each frame
param str font: font file
param int text_size: size of label text
param str text_color: color to use for the text.
"""

# Render images if they have not been already
if not self._layers_rendered:
self.render()

input_files = []
for index in self._indices:
input_files.append(self._base_filename_dict[index])

save_gif(
input_files,
filename,
duration=duration,
label=label,
labels=self._labels,
font=font,
text_size=text_size,
text_color=text_color,
)

# Display the GIF
return filename
47 changes: 0 additions & 47 deletions python/grass/jupyter/seriesmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from .map import Map
from .region import RegionManagerForSeries
from .utils import save_gif
from .baseseriesmap import BaseSeriesMap


Expand Down Expand Up @@ -165,49 +164,3 @@ def render(self):
)
tasks = [(i,) for i in range(self.baseseries)]
self._render(tasks)

def save(
self,
filename,
duration=500,
label=True,
font=None,
text_size=12,
text_color="gray",
):
"""
Creates a GIF animation of rendered layers.
Text color must be in a format accepted by PIL ImageColor module. For supported
formats, visit:
https://pillow.readthedocs.io/en/stable/reference/ImageColor.html#color-names
param str filename: name of output GIF file
param int duration: time to display each frame; milliseconds
param bool label: include label on each frame
param str font: font file
param int text_size: size of label text
param str text_color: color to use for the text
"""

# Render images if they have not been already
if not self._layers_rendered:
self.render()

tmp_files = []
for file in self._base_filename_dict.values():
tmp_files.append(file)

save_gif(
tmp_files,
filename,
duration=duration,
label=label,
labels=self._labels,
font=font,
text_size=text_size,
text_color=text_color,
)

# Display the GIF
return filename
47 changes: 0 additions & 47 deletions python/grass/jupyter/timeseriesmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from .map import Map
from .region import RegionManagerForTimeSeries
from .utils import save_gif
from .baseseriesmap import BaseSeriesMap


Expand Down Expand Up @@ -312,49 +311,3 @@ def render(self):
filename = os.path.join(self._tmpdir.name, f"{layer}.png")
tasks.append((date, layer, filename))
self._render(tasks)

def save(
self,
filename,
duration=500,
label=True,
font="DejaVuSans.ttf",
text_size=12,
text_color="gray",
):
"""
Creates a GIF animation of rendered layers.
Text color must be in a format accepted by PIL ImageColor module. For supported
formats, visit:
https://pillow.readthedocs.io/en/stable/reference/ImageColor.html#color-names
param str filename: name of output GIF file
param int duration: time to display each frame; milliseconds
param bool label: include date/time stamp on each frame
param str font: font file
param int text_size: size of date/time text
param str text_color: color to use for the text.
"""

# Render images if they have not been already
if not self._layers_rendered:
self.render()

input_files = []
for date in self._labels:
input_files.append(self._base_filename_dict[date])

save_gif(
input_files,
filename,
duration=duration,
label=label,
labels=self._labels,
font=font,
text_size=text_size,
text_color=text_color,
)

# Display the GIF
return filename
Loading