-
-
Notifications
You must be signed in to change notification settings - Fork 655
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 #2387 from HalfWhitt/image_plugin
Support for custom image format plugins
- Loading branch information
Showing
19 changed files
with
311 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Toga can now be extended, via plugins, to create Toga Images from external image classes (and vice-versa). |
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
Empty file.
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,40 @@ | ||
from __future__ import annotations | ||
|
||
from io import BytesIO | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from toga.images import BytesLike | ||
|
||
# Presumably, other converter plugins will be included with, or only installed | ||
# alongside, the packages they're for. But since this is provided in Toga, we need to | ||
# check if Pillow is actually installed, and disable this plugin otherwise. | ||
try: | ||
import PIL.Image | ||
|
||
PIL_imported = True | ||
|
||
except ImportError: # pragma: no cover | ||
PIL_imported = False | ||
|
||
|
||
class PILConverter: | ||
image_class = PIL.Image.Image if PIL_imported else None | ||
|
||
@staticmethod | ||
def convert_from_format(image_in_format: PIL.Image.Image) -> bytes: | ||
buffer = BytesIO() | ||
image_in_format.save(buffer, format="png", compress_level=0) | ||
return buffer.getvalue() | ||
|
||
@staticmethod | ||
def convert_to_format( | ||
data: BytesLike, | ||
image_class: type(PIL.Image.Image), | ||
) -> PIL.Image.Image: | ||
# PIL Images aren't designed to be subclassed, so no implementation is necessary | ||
# for a supplied format. | ||
buffer = BytesIO(data) | ||
with PIL.Image.open(buffer) as pil_image: | ||
pil_image.load() | ||
return pil_image |
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
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
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 |
---|---|---|
|
@@ -11,3 +11,4 @@ Reference | |
widgets_by_platform | ||
api/index | ||
style/index | ||
plugins/index |
Oops, something went wrong.