Skip to content

Commit

Permalink
Finalize SpriteSystem.from_image PIL support from #255 (#256)
Browse files Browse the repository at this point in the history
* Add PIL.Image as an expected argument argument for the from_image function

Allowing a PIL Image in a program to be used as a sprite. A PIL.Image can be directly inputted to the from_image functinon in spritesystem.py

* Add tests, avoid hard PIL dependency

* Updated news.rst and AUTHORS.txt

Co-authored-by: Minhal Valiya Peedikakkal <35574445+minhalvp@users.noreply.github.com>
  • Loading branch information
a-hurst and minhalvp committed Jan 8, 2023
1 parent fc08867 commit 49aa61b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Thanks to everyone else for their assistance, support, fixes and improvements:
* mgorny
* Michael McCandless
* Mihail Latyshov
* Minhal Valiya Peedikakkal
* otus
* Paul Vallet
* Piper Thunstrom
Expand Down
2 changes: 2 additions & 0 deletions doc/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ New Features:
* Cache :class:`sdl2.ext.Texture` sizes for faster retrieval, improving render
performance in some cases.
* Updated to wrap new functions and constants in SDL2 2.26.0 (PR #252 & #253).
* :meth:`~sdl2.ext.SpriteFactory.from_image` now accepts ``PIL.Image.Image``
objects directly in addition to image filepaths (PR #255 & #256).

Fixed Bugs:

Expand Down
12 changes: 8 additions & 4 deletions sdl2/ext/spritesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .err import SDLError
from .compat import isiterable
from .ebs import System
from .image import load_image
from .image import load_image, pillow_to_surface, _HASPIL
from .renderer import Renderer
from .sprite import Sprite, SoftwareSprite, TextureSprite
from .window import Window
Expand Down Expand Up @@ -61,9 +61,13 @@ def create_sprite_render_system(self, *args, **kwargs):
else:
return SoftwareSpriteRenderSystem(*args, **kwargs)

def from_image(self, fname):
"""Creates a Sprite from the passed image file."""
return self.from_surface(load_image(fname), True)
def from_image(self, img):
"""Creates a Sprite from the passed PIL.Image or image file name."""
if _HASPIL:
from PIL.Image import Image
if isinstance(img, Image):
return self.from_surface(pillow_to_surface(img))
return self.from_surface(load_image(img), True)

def from_surface(self, tsurface, free=False):
"""Creates a Sprite from the passed SDL_Surface.
Expand Down
15 changes: 15 additions & 0 deletions sdl2/test/sdl2ext_spritesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
from sdl2.pixels import SDL_MapRGBA
from sdl2.error import SDL_GetError, SDL_ClearError

try:
import PIL
_HASPIL = True
except ImportError:
_HASPIL = False

RESOURCES = Resources(__file__, "resources")

BLACK = (0, 0, 0, 255)
Expand Down Expand Up @@ -144,6 +150,15 @@ def test_from_image(self, with_sdl):
ssprite = sfactory.from_image(imgname)
assert isinstance(ssprite, sdl2ext.SoftwareSprite)

if _HASPIL:
from PIL import Image
imgname = RESOURCES.get_path("surfacetest.png")
img = Image.open(imgname)
tsprite = tfactory.from_image(img)
assert isinstance(tsprite, sdl2ext.TextureSprite)
ssprite = sfactory.from_image(img)
assert isinstance(ssprite, sdl2ext.SoftwareSprite)

for factory in (tfactory, sfactory):
with pytest.raises(ValueError):
factory.from_image(None)
Expand Down

0 comments on commit 49aa61b

Please sign in to comment.