Skip to content

Commit 49aa61b

Browse files
a-hurstminhalvp
andauthored
Finalize SpriteSystem.from_image PIL support from #255 (#256)
* 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 <[email protected]>
1 parent fc08867 commit 49aa61b

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

AUTHORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Thanks to everyone else for their assistance, support, fixes and improvements:
3838
* mgorny
3939
* Michael McCandless
4040
* Mihail Latyshov
41+
* Minhal Valiya Peedikakkal
4142
* otus
4243
* Paul Vallet
4344
* Piper Thunstrom

doc/news.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ New Features:
2323
* Cache :class:`sdl2.ext.Texture` sizes for faster retrieval, improving render
2424
performance in some cases.
2525
* Updated to wrap new functions and constants in SDL2 2.26.0 (PR #252 & #253).
26+
* :meth:`~sdl2.ext.SpriteFactory.from_image` now accepts ``PIL.Image.Image``
27+
objects directly in addition to image filepaths (PR #255 & #256).
2628

2729
Fixed Bugs:
2830

sdl2/ext/spritesystem.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .err import SDLError
55
from .compat import isiterable
66
from .ebs import System
7-
from .image import load_image
7+
from .image import load_image, pillow_to_surface, _HASPIL
88
from .renderer import Renderer
99
from .sprite import Sprite, SoftwareSprite, TextureSprite
1010
from .window import Window
@@ -61,9 +61,13 @@ def create_sprite_render_system(self, *args, **kwargs):
6161
else:
6262
return SoftwareSpriteRenderSystem(*args, **kwargs)
6363

64-
def from_image(self, fname):
65-
"""Creates a Sprite from the passed image file."""
66-
return self.from_surface(load_image(fname), True)
64+
def from_image(self, img):
65+
"""Creates a Sprite from the passed PIL.Image or image file name."""
66+
if _HASPIL:
67+
from PIL.Image import Image
68+
if isinstance(img, Image):
69+
return self.from_surface(pillow_to_surface(img))
70+
return self.from_surface(load_image(img), True)
6771

6872
def from_surface(self, tsurface, free=False):
6973
"""Creates a Sprite from the passed SDL_Surface.

sdl2/test/sdl2ext_spritesystem_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
from sdl2.pixels import SDL_MapRGBA
1212
from sdl2.error import SDL_GetError, SDL_ClearError
1313

14+
try:
15+
import PIL
16+
_HASPIL = True
17+
except ImportError:
18+
_HASPIL = False
19+
1420
RESOURCES = Resources(__file__, "resources")
1521

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

153+
if _HASPIL:
154+
from PIL import Image
155+
imgname = RESOURCES.get_path("surfacetest.png")
156+
img = Image.open(imgname)
157+
tsprite = tfactory.from_image(img)
158+
assert isinstance(tsprite, sdl2ext.TextureSprite)
159+
ssprite = sfactory.from_image(img)
160+
assert isinstance(ssprite, sdl2ext.SoftwareSprite)
161+
147162
for factory in (tfactory, sfactory):
148163
with pytest.raises(ValueError):
149164
factory.from_image(None)

0 commit comments

Comments
 (0)