Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image function #89

Merged
merged 2 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions spark/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ def line_width(self, *args): pass
@extern
def stroke_width(self, *args): pass

# From util.helper_functions.image_functions
@extern
def image(self, *args): pass

### Helper Functions ###

# From util.helper_functions.misc_functions
Expand Down
1 change: 1 addition & 0 deletions spark/util/core_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .helper_functions.square_functions import *
from .helper_functions.text_functions import *
from .helper_functions.triangle_functions import *
from .helper_functions.image_functions import *

from .helper_functions.keyboard_functions import *

Expand Down
39 changes: 39 additions & 0 deletions spark/util/helper_functions/image_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Dict, Tuple
if TYPE_CHECKING:
from ...core import Core

from ..decorators import *
from numbers import Real
from ipywidgets import Image
from ipycanvas import Canvas


_loaded_images: Dict[Tuple[str, int, int], Canvas] = {}


@validate_args([str, Real, Real, Real, Real])
@ignite_global
def helper_image(self: Core, *args):
filename: str = args[0]
x: int = int(args[1])
y: int = int(args[2])
w: int = int(args[3])
h: int = int(args[4])

key = (filename, abs(w), abs(h))

if key not in _loaded_images:
_loaded_images[key] = Canvas(width=abs(w), height=abs(h))
_loaded_images[key].draw_image(Image.from_file(filename, width=abs(w), height=abs(h)), 0, 0, abs(w), abs(h))
self.canvas.translate(x, y)
if w < 0:
self.canvas.scale(-1, 1)
if h < 0:
self.canvas.scale(1, -1)
self.canvas.draw_image(_loaded_images[key], 0, 0)
if h < 0:
self.canvas.scale(1, -1)
if w < 0:
self.canvas.scale(-1, 1)
self.canvas.translate(-x, -y)
Loading