Skip to content

Commit c4dc413

Browse files
committed
added support for base64 textures.
1 parent ed95516 commit c4dc413

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

ursina/texture.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ class Texture():
1515

1616
def __init__(self, value, filtering='default'):
1717

18-
if isinstance(value, str):
18+
if isinstance(value, str) and value.startswith('data:image/png;base64'):
19+
import base64
20+
from PIL import Image
21+
from io import BytesIO
22+
base64_data = value.lstrip('data:image/png;base64') # remove prefix
23+
value = Image.open(BytesIO(base64.b64decode(base64_data))).convert('RGBA')
24+
25+
elif isinstance(value, str):
1926
value = Path(value)
2027

2128
if isinstance(value, Path):
@@ -244,7 +251,7 @@ def input(key):
244251
e.texture = 'test_tileset'
245252
# e.texture.set_pixels([e for e in e.texture.get_pixels()])
246253
e.texture.apply()
247-
254+
248255
# tex = Texture.new((512,512), (255,0,0))
249256

250257
pixels = e.texture.pixels
@@ -253,5 +260,9 @@ def input(key):
253260
for (x,y), value in enumerate_2d(pixels):
254261
new_grid[x][y] = int(color.rgba32(*value).v > .5)
255262

263+
264+
texture_from_base64_string = Entity(model='cube', y=1.5, scale=1, texture=Texture('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAJ0lEQVR4nGK5d/gdAwODArcEkGRiQAKMmut0gdTX/YroMoAAAAD//8caBbV8Qu6pAAAAAElFTkSuQmCC'))
265+
EditorCamera()
266+
256267
# print(new_grid)
257268
app.run()

0 commit comments

Comments
 (0)