-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add new base IImage interface and allow it in Image traits. * Remove methods from IImageResource. * Update comments. * Improve docstrings. * Add image helper methods and tests. * Add ArrayImage class (no tests yet, but very thin). * Add PIL/Pillow-based IImage implementation. * Improvements to PILImage based on improvements in underlying image code. * Fix bad import. * Fix bad toolkit syntax. * Test imports of image helpers work. * Fix toolkit imports. * Fix copyrights and remove __future__ imports. * Add tests for array image class. * Improvements to tests. * Fix missing not in size check. * Fix conflicting imports. * Missed a marge conflict. * Make pillow an optional dependency; fix a bug. * Apply suggestions from code review Co-authored-by: Poruri Sai Rahul <[email protected]> * Restore spaces. * Remove excess whitespace. * Missed one suggested change Co-authored-by: Poruri Sai Rahul <[email protected]> Co-authored-by: Poruri Sai Rahul <[email protected]>
- Loading branch information
1 parent
997c78f
commit c6616a3
Showing
8 changed files
with
268 additions
and
2 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 |
---|---|---|
|
@@ -98,6 +98,7 @@ | |
"numpy", | ||
"pygments", | ||
"coverage", | ||
"pillow", | ||
} | ||
|
||
source_dependencies = { | ||
|
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,73 @@ | ||
# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX | ||
# All rights reserved. | ||
# | ||
# This software is provided without warranty under the terms of the BSD | ||
# license included in LICENSE.txt and may be redistributed only under | ||
# the conditions described in the aforementioned license. The license | ||
# is also available online at http://www.enthought.com/licenses/BSD.txt | ||
# | ||
# Thanks for using Enthought open source! | ||
""" The interface for a PIL Image. """ | ||
|
||
from traits.api import HasStrictTraits, Instance | ||
|
||
from pyface.i_image import IImage | ||
|
||
|
||
class IPILImage(IImage): | ||
""" The interface for a image that wraps a PIL Image. | ||
""" | ||
|
||
# 'IPILImage' interface -------------------------------------------- | ||
|
||
#: The PIL Image instance. | ||
image = Instance("PIL.Image.Image") | ||
|
||
|
||
class MPILImage(HasStrictTraits): | ||
""" The base implementation mixin for a image that wraps a PIL Image. | ||
""" | ||
|
||
# 'IPILImage' interface -------------------------------------------- | ||
|
||
#: The PIL Image instance. | ||
image = Instance("PIL.Image.Image") | ||
|
||
def __init__(self, image, **traits): | ||
super().__init__(image=image, **traits) | ||
|
||
def create_bitmap(self, size=None): | ||
""" Creates a bitmap image for this image. | ||
Parameters | ||
---------- | ||
size : (int, int) or None | ||
The desired size as a (width, height) tuple, or None if wanting | ||
default image size. | ||
Returns | ||
------- | ||
image : bitmap | ||
The toolkit bitmap corresponding to the image and the specified | ||
size. | ||
""" | ||
from pyface.util.image_helpers import image_to_bitmap | ||
return image_to_bitmap(self.create_image(size)) | ||
|
||
def create_icon(self, size=None): | ||
""" Creates an icon for this image. | ||
Parameters | ||
---------- | ||
size : (int, int) or None | ||
The desired size as a (width, height) tuple, or None if wanting | ||
default icon size. | ||
Returns | ||
------- | ||
image : icon | ||
The toolkit image corresponding to the image and the specified | ||
size as an icon. | ||
""" | ||
from pyface.util.image_helpers import bitmap_to_icon | ||
return bitmap_to_icon(self.create_bitmap(size)) |
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 @@ | ||
# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX | ||
# All rights reserved. | ||
# | ||
# This software is provided without warranty under the terms of the BSD | ||
# license included in LICENSE.txt and may be redistributed only under | ||
# the conditions described in the aforementioned license. The license | ||
# is also available online at http://www.enthought.com/licenses/BSD.txt | ||
# | ||
# Thanks for using Enthought open source! | ||
|
||
""" The implementation of a IPILImage. """ | ||
|
||
|
||
# Import the toolkit specific version. | ||
|
||
|
||
from .toolkit import toolkit_object | ||
|
||
PILImage = toolkit_object("pil_image:PILImage") |
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,49 @@ | ||
# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX | ||
# All rights reserved. | ||
# | ||
# This software is provided without warranty under the terms of the BSD | ||
# license included in LICENSE.txt and may be redistributed only under | ||
# the conditions described in the aforementioned license. The license | ||
# is also available online at http://www.enthought.com/licenses/BSD.txt | ||
# | ||
# Thanks for using Enthought open source! | ||
|
||
|
||
import os | ||
import unittest | ||
|
||
# importlib.resources is new in Python 3.7, and importlib.resources.files is | ||
# new in Python 3.9, so for Python < 3.9 we must rely on the 3rd party | ||
# importlib_resources package. | ||
try: | ||
from importlib.resources import files | ||
except ImportError: | ||
from importlib_resources import files | ||
|
||
from PIL import Image | ||
|
||
from ..pil_image import PILImage | ||
|
||
IMAGE_PATH = os.fspath(files("pyface.tests") / "images" / "core.png") | ||
|
||
|
||
class TestPILImage(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.pil_image = Image.open(IMAGE_PATH) | ||
|
||
def test_create_image(self): | ||
image = PILImage(self.pil_image) | ||
toolkit_image = image.create_image() | ||
self.assertIsNotNone(toolkit_image) | ||
self.assertEqual(image.image, self.pil_image) | ||
|
||
def test_create_bitmap(self): | ||
image = PILImage(self.pil_image) | ||
bitmap = image.create_bitmap() | ||
self.assertIsNotNone(bitmap) | ||
|
||
def test_create_icon(self): | ||
image = PILImage(self.pil_image) | ||
icon = image.create_icon() | ||
self.assertIsNotNone(icon) |
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,48 @@ | ||
# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX | ||
# All rights reserved. | ||
# | ||
# This software is provided without warranty under the terms of the BSD | ||
# license included in LICENSE.txt and may be redistributed only under | ||
# the conditions described in the aforementioned license. The license | ||
# is also available online at http://www.enthought.com/licenses/BSD.txt | ||
# | ||
# Thanks for using Enthought open source! | ||
|
||
from pyface.qt.QtGui import QIcon, QPixmap | ||
|
||
from traits.api import provides | ||
|
||
from pyface.i_pil_image import IPILImage, MPILImage | ||
from pyface.ui.qt4.util.image_helpers import resize_image | ||
|
||
|
||
@provides(IPILImage) | ||
class PILImage(MPILImage): | ||
""" The toolkit specific implementation of a PILImage. | ||
""" | ||
|
||
# ------------------------------------------------------------------------ | ||
# 'IImage' interface. | ||
# ------------------------------------------------------------------------ | ||
|
||
def create_image(self, size=None): | ||
""" Creates a Qt image for this image. | ||
Parameters | ||
---------- | ||
size : (int, int) or None | ||
The desired size as a (width, height) tuple, or None if wanting | ||
default image size. | ||
Returns | ||
------- | ||
image : QImage | ||
The toolkit image corresponding to the image and the specified | ||
size. | ||
""" | ||
from PIL.ImageQt import ImageQt | ||
image = ImageQt(self.image) | ||
if size is not None: | ||
return resize_image(image, size) | ||
else: | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX | ||
# All rights reserved. | ||
# | ||
# This software is provided without warranty under the terms of the BSD | ||
# license included in LICENSE.txt and may be redistributed only under | ||
# the conditions described in the aforementioned license. The license | ||
# is also available online at http://www.enthought.com/licenses/BSD.txt | ||
# | ||
# Thanks for using Enthought open source! | ||
|
||
import wx | ||
|
||
from traits.api import provides | ||
|
||
from pyface.i_pil_image import IPILImage, MPILImage | ||
from pyface.ui.wx.util.image_helpers import resize_image | ||
|
||
|
||
@provides(IPILImage) | ||
class PILImage(MPILImage): | ||
""" The toolkit specific implementation of a PILImage. | ||
""" | ||
|
||
# ------------------------------------------------------------------------ | ||
# 'IImage' interface. | ||
# ------------------------------------------------------------------------ | ||
|
||
def create_image(self, size=None): | ||
""" Creates a Wx image for this image. | ||
Parameters | ||
---------- | ||
size : (int, int) or None | ||
The desired size as a (width, height) tuple, or None if wanting | ||
default image size. | ||
Returns | ||
------- | ||
image : wx.Image | ||
The toolkit image corresponding to the image and the specified | ||
size. | ||
""" | ||
image = self.image | ||
wx_image = wx.EmptyImage(self.image.size[0], self.image.size[1]) | ||
wx_image.SetData(image.convert("RGB").tobytes()) | ||
if image.mode == "RGBA": | ||
wx_image.InitAlpha() | ||
wx_image.SetAlpha(image.getchannel("A").tobytes()) | ||
if size is not None: | ||
return resize_image(wx_image, size) | ||
else: | ||
return wx_image |