-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,7 @@ instance/ | |
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
./target/ | ||
|
||
# IPython Notebook | ||
.ipynb_checkpoints | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from nasabot.geo.animation.target.gif import Gif | ||
|
||
__all__ = [Gif] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import logging | ||
import os | ||
from nasabot.geo.animation.target import \ | ||
imageio_animation, wand_animation | ||
from slugify import slugify | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def uri_2_filename(uri): | ||
""" | ||
convert uri to related filename | ||
:param uri: | ||
:return: | ||
""" | ||
*first, ext = uri.split('.') | ||
return slugify('.'.join(first)) + '.' + ext | ||
|
||
|
||
class Gif: | ||
def __init__(self, target_filename=''): | ||
self.target_filename = target_filename | ||
self.root_path = os.path.dirname(target_filename) | ||
|
||
# TODO: should make it selectable | ||
self.animation_provider = imageio_animation | ||
# self.animation_provider = wand_animation | ||
|
||
def get_filename_by_url(self, url): | ||
return os.path.join(self.root_path, uri_2_filename(url)) | ||
|
||
async def save(self, files): | ||
await self.animation_provider.animate( | ||
os.path.join(self.target_filename), files) | ||
logger.info('# gif file is saved to {}'.format(self.target_filename)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import logging | ||
import imageio | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def animate(export_filename, file_sequence): | ||
""" | ||
:param file_sequence: | ||
:return: | ||
""" | ||
logger.debug('# animate sequence of files') | ||
logger.debug(file_sequence) | ||
|
||
frames = [imageio.imread(file_name) for file_name in file_sequence] | ||
imageio.mimsave(export_filename, frames, 'GIF', | ||
# fps=30, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import logging | ||
from wand.image import Image | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def animate(export_filename, file_sequence): | ||
""" | ||
20-40% slower than imageio :( | ||
:param file_sequence: | ||
:return: | ||
""" | ||
logger.debug('animate sequence of files') | ||
logger.debug(file_sequence) | ||
|
||
with Image() as gif: | ||
for file_name in file_sequence: | ||
with Image(filename=file_name) as frame: | ||
gif.sequence.append(frame) | ||
gif.type = 'optimize' | ||
gif.save(filename=export_filename) |