mirror of
https://github.com/3b1b/manim.git
synced 2025-07-30 13:34:19 +08:00
56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
import numpy as np
|
|
import itertools as it
|
|
import os
|
|
from PIL import Image
|
|
from random import random
|
|
|
|
from helpers import *
|
|
from mobject import Mobject
|
|
from point_cloud_mobject import PMobject
|
|
|
|
class ImageMobject(Mobject):
|
|
"""
|
|
Automatically filters out black pixels
|
|
"""
|
|
CONFIG = {
|
|
"filter_color" : "black",
|
|
"invert" : False,
|
|
# "use_cache" : True,
|
|
"height": 2.0,
|
|
"image_mode" : "RGBA"
|
|
}
|
|
def __init__(self, filename_or_array, **kwargs):
|
|
digest_config(self, kwargs)
|
|
if isinstance(filename_or_array, str):
|
|
path = get_full_image_path(filename_or_array)
|
|
image = Image.open(path).convert(self.image_mode)
|
|
self.pixel_array = np.array(image)
|
|
else:
|
|
self.pixel_array = np.array(filename_or_array)
|
|
Mobject.__init__(self, **kwargs)
|
|
|
|
|
|
def init_points(self):
|
|
#Corresponding corners of image are fixed to these
|
|
#Three points
|
|
self.points = np.array([
|
|
UP+LEFT,
|
|
UP+RIGHT,
|
|
DOWN+LEFT,
|
|
])
|
|
self.center()
|
|
self.scale_to_fit_height(self.height)
|
|
h, w = self.pixel_array.shape[:2]
|
|
self.stretch_to_fit_width(self.height*w/h)
|
|
|
|
def set_opacity(self, alpha):
|
|
self.pixel_array[:,:,3] = int(255*alpha)
|
|
return self
|
|
|
|
def fade(self, darkness = 0.5):
|
|
self.set_opacity(1 - darkness)
|
|
return self
|
|
|
|
|
|
|