-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Implement :class:~.OpenGLImageMobject
#2534
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0d60da5
initial commit
icedcoffeeee ad3dcfa
added typing
icedcoffeeee 51683b1
unused import
icedcoffeeee ab6cf9c
explicit imports
icedcoffeeee 9cfc3f4
new typing style
icedcoffeeee 777e9f3
Merge branch 'main' into image-opengl
icedcoffeeee 98cc2e1
convert to correct type
icedcoffeeee 2019228
Merge remote-tracking branch 'upstream/main' into image-opengl
icedcoffeeee 7aea562
update file structure
icedcoffeeee 50b8a06
Merge branch 'main' into image-opengl
icedcoffeeee 0a15445
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8ebd15d
Added support for Path and image_mode parameter
icedcoffeeee 9694e0e
remove duplicate function
icedcoffeeee 371b2ac
Merge remote-tracking branch 'upstream/main' into image-opengl
icedcoffeeee a668221
let moderngl accept PIL Images only
icedcoffeeee f85de50
allow custom resampling
icedcoffeeee f0584ed
allow greyscale
icedcoffeeee a517a30
Merge branch 'main' into image-opengl
icedcoffeeee 5522989
Merge branch 'main' into image-opengl
naveen521kk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,80 @@ | ||
from __future__ import annotations | ||
|
||
__all__ = [ | ||
"OpenGLImageMobject", | ||
] | ||
|
||
from pathlib import Path | ||
|
||
import numpy as np | ||
from PIL import Image | ||
|
||
from manim.mobject.opengl.opengl_surface import OpenGLSurface, OpenGLTexturedSurface | ||
from manim.utils.images import get_full_raster_image_path | ||
|
||
|
||
class OpenGLImageMobject(OpenGLTexturedSurface): | ||
def __init__( | ||
self, | ||
filename_or_array: str | Path | np.ndarray, | ||
width: float = None, | ||
height: float = None, | ||
image_mode: str = "RGBA", | ||
resampling_algorithm: int = Image.BICUBIC, | ||
opacity: float = 1, | ||
gloss: float = 0, | ||
shadow: float = 0, | ||
**kwargs, | ||
): | ||
self.image = filename_or_array | ||
self.resampling_algorithm = resampling_algorithm | ||
if type(filename_or_array) == np.ndarray: | ||
self.size = self.image.shape[1::-1] | ||
elif isinstance(filename_or_array, (str, Path)): | ||
path = get_full_raster_image_path(filename_or_array) | ||
self.size = Image.open(path).size | ||
|
||
if width is None and height is None: | ||
width = 4 * self.size[0] / self.size[1] | ||
height = 4 | ||
if height is None: | ||
height = width * self.size[1] / self.size[0] | ||
if width is None: | ||
width = height * self.size[0] / self.size[1] | ||
|
||
surface = OpenGLSurface( | ||
lambda u, v: np.array([u, v, 0]), | ||
[-width / 2, width / 2], | ||
[-height / 2, height / 2], | ||
opacity=opacity, | ||
gloss=gloss, | ||
shadow=shadow, | ||
) | ||
|
||
super().__init__( | ||
surface, | ||
self.image, | ||
image_mode=image_mode, | ||
opacity=opacity, | ||
gloss=gloss, | ||
shadow=shadow, | ||
**kwargs, | ||
) | ||
|
||
def get_image_from_file( | ||
self, | ||
image_file: str | Path | np.ndarray, | ||
image_mode: str, | ||
): | ||
if isinstance(image_file, (str, Path)): | ||
return super().get_image_from_file(image_file, image_mode) | ||
else: | ||
return ( | ||
Image.fromarray(image_file.astype("uint8")) | ||
.convert(image_mode) | ||
.resize( | ||
np.array(image_file.shape[:2]) | ||
* 200, # assumption of 200 ppmu (pixels per manim unit) would suffice | ||
resample=self.resampling_algorithm, | ||
) | ||
) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.