From ae590e7fdeb7be43747d5359f51ef3b98bbb8f0f Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Wed, 12 Aug 2020 13:23:21 -0700 Subject: [PATCH] Add point_to_rgb --- manimlib/mobject/types/image_mobject.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/manimlib/mobject/types/image_mobject.py b/manimlib/mobject/types/image_mobject.py index 266897c1..fd30b0dd 100644 --- a/manimlib/mobject/types/image_mobject.py +++ b/manimlib/mobject/types/image_mobject.py @@ -5,6 +5,7 @@ from PIL import Image from manimlib.constants import * from manimlib.mobject.mobject import Mobject from manimlib.utils.bezier import interpolate +from manimlib.utils.bezier import inverse_interpolate from manimlib.utils.images import get_full_raster_image_path from manimlib.utils.iterables import listify @@ -58,6 +59,22 @@ class ImageMobject(Mobject): mobject1.opacity, mobject2.opacity, alpha ) + def point_to_rgb(self, point): + x0, y0 = self.get_corner(UL)[:2] + x1, y1 = self.get_corner(DR)[:2] + x_alpha = inverse_interpolate(x0, x1, point[0]) + y_alpha = inverse_interpolate(y0, y1, point[1]) + if not (0 <= x_alpha <= 1) and (0 <= y_alpha <= 1): + # TODO, raise smarter exception + raise Exception("Cannot sample color from outside an image") + + pw, ph = self.image.size + rgb = self.image.getpixel(( + int((pw - 1) * x_alpha), + int((ph - 1) * y_alpha), + )) + return np.array(rgb) / 255 + def get_shader_data(self): data = self.get_blank_shader_data_array(len(self.points)) data["point"] = self.points