-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from shahriyardx/feat/gif
added gif editing feature
- Loading branch information
Showing
12 changed files
with
674 additions
and
614 deletions.
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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
furo==2023.3.27 | ||
aiocache==0.12.1 | ||
aiohttp==3.8.4 | ||
Pillow==9.5.0 | ||
Pillow==10.1.0 | ||
requests==2.30.0 | ||
typing_extensions==4.5.0 |
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
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,60 @@ | ||
from __future__ import annotations | ||
|
||
from io import BytesIO | ||
from pathlib import Path | ||
from typing import List, Tuple, Union | ||
|
||
from PIL import Image as PilImage, ImageSequence | ||
from PIL.GifImagePlugin import GifImageFile | ||
|
||
from .editor import Editor | ||
|
||
|
||
class GifEditor: | ||
def __init__(self, image: Union[str, BytesIO, Path, GifImageFile]): | ||
if isinstance(image, (str, BytesIO, Path)): | ||
self.image = PilImage.open(image) | ||
if isinstance(image, GifImageFile): | ||
self.image = image | ||
|
||
self.original_frames = ImageSequence.Iterator(self.image) | ||
self.frames: List[Editor] = list( | ||
map(lambda x: Editor(x), self.original_frames) | ||
) | ||
self.size: Tuple[int, int] = self.image.size | ||
|
||
def __getattr__(self, name): | ||
def wrapper(*args, **kwargs): | ||
for frame in self.frames: | ||
getattr(frame, name)(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
@property | ||
def image_bytes(self) -> BytesIO: | ||
"""Return image bytes | ||
Returns | ||
------- | ||
BytesIO | ||
Bytes from the image of Editor | ||
""" | ||
_bytes = BytesIO() | ||
images = list(map(lambda e: e.image, self.frames)) | ||
images[0].save(_bytes, "GIF", save_all=True, append_images=images[1:]) | ||
|
||
_bytes.seek(0) | ||
return _bytes | ||
|
||
def save(self, fp, **kwargs): | ||
"""Save the image | ||
Parameters | ||
---------- | ||
fp : str | ||
File path | ||
""" | ||
images = list(map(lambda e: e.image, self.frames)) | ||
images[0].save( | ||
fp, "GIF", save_all=True, append_images=images[1:], **kwargs | ||
) |
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
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
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
Oops, something went wrong.