'Crop Manim Animation to Match Content Size
For the following Manim code, I would like to (automatically) produce an animation that only contains the equation eq with no empty space surrounding it. How can I accomplish this?
from manim import *
class EquationScene(Scene):
def construct(self):
eq = MathTex('a^2 + b^2 = c^2')
self.play(Write(eq))
# Crop image here, somehow?
If this cannot be done purely in Manim, I'm open to other approaches, such as using ImageMagick to crop the images. (The method must be automatic, however. I obviously don't want to have to manually crop every image each time I modify it.)
See also a related question here.
Solution 1:[1]
Here is a snippet which will, upon rendering, produce an empty output image -- while at the same time writing a file captured_mobject.png in your working directory which contains the desired output.
class MobjectCapture(Scene):
def construct(self):
mob = Text("This is some text that we would like to capture.")
padding = 0
m_width = mob.width + padding
m_height = mob.height + padding
p_width = int(m_width * config.pixel_width / config.frame_width)
with tempconfig({
"frame_width": m_width,
"frame_height": m_height,
"pixel_width": p_width,
"pixel_height": int(p_width * m_height / m_width)
}):
img = mob.get_image()
img.save("captured_mobject.png")?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Benjamin Hackl |
