3b1b-manim/mobject/image_mobject.py

56 lines
1.4 KiB
Python
Raw Normal View History

2015-04-03 16:41:25 -07:00
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
2016-04-14 19:30:47 -07:00
from point_cloud_mobject import PMobject
2015-04-03 16:41:25 -07:00
2017-09-18 17:15:49 -07:00
class ImageMobject(Mobject):
2015-04-03 16:41:25 -07:00
"""
Automatically filters out black pixels
"""
2016-02-27 16:32:53 -08:00
CONFIG = {
"filter_color" : "black",
"invert" : False,
2017-09-18 17:15:49 -07:00
# "use_cache" : True,
"height": 2.0,
"image_mode" : "RGBA"
}
2017-09-18 17:15:49 -07:00
def __init__(self, filename_or_array, **kwargs):
digest_config(self, kwargs)
2017-09-18 17:15:49 -07:00
if isinstance(filename_or_array, str):
path = get_full_image_path(filename_or_array)
image = Image.open(path).convert(self.image_mode)
2017-09-18 17:15:49 -07:00
self.pixel_array = np.array(image)
else:
self.pixel_array = np.array(filename_or_array)
Mobject.__init__(self, **kwargs)
2017-09-18 17:15:49 -07:00
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