Search code examples
pythonmanim

Manim png rendered is cropped


I'm trying to plot a table from numbers upto 100, but when the png is rendered (because there's still no animation), the image is cropped and I don't know if it's necesary to do some zoom out to the scene or what. I tried with the flag -r but it only changes the size of the image, it still looks cropped.

from manim import *

class DrawTable(Scene):
   def construct(self):
       N = 100
       ROWS, COLS = 10, 10

       vals = np.arange(1,N+1).reshape(ROWS,COLS)
       table = IntegerTable(
           vals,
           include_outer_lines=True
       )

       self.add(table)    

        

And the png: png rendered


Solution

  • You can use .scale(value) to manually rescale it.

    table = table.scale(0.5)
    

    enter image description here


    If you use print( dir(table) ) then you can see all functions avaliable for table and there is .scale() but also .scale_to_fit_width(width) and .scale_to_fit_height(height) which you can use with config.frame_width, config.frame_height. But you have to choose which one to use. For some (table and screen) sizes you will need fit_width and for others fit_height.

    table = table.scale_to_fit_width(config.frame_width)
    

    enter image description here

    table = table.scale_to_fit_height(config.frame_height)
    

    enter image description here


    You can also calculate scale between table.width and config.frame_width, and scale between table.height and config.frame_height, and use min() or max() to choose correct scale. But again for some (table and screen) sizes you will need min() and for others `max().

    scale_x = config.frame_width/table.width
    scale_y = config.frame_height/table.height
    
    scale = min([scale_x, scale_y])
    
    table = table.scale(scale)
    

    Full code for tests.

    Tested with Manim Community 0.12.0
    Not tested with Original Manim create by 3Blue1Brown

    from manim import *
    
    class DrawTable(Scene):
    
        def construct(self):
        
            #print(config)
            
            N = 100
            ROWS, COLS = 10, 10
    
            vals = np.arange(1,N+1).reshape(ROWS,COLS)
           
            table = IntegerTable(
                vals,
                include_outer_lines=True
            )
    
            print("\n".join(dir(table)))  # display all functions 
    
            # --- manually ---
    
            #table = table.scale(0.5)
    
            # --- fit ---
    
            #print(config.frame_width, config.frame_height)
            
            #table = table.scale_to_fit_width(config.frame_width)
            #table = table.scale_to_fit_height(config.frame_height)
            
            # --- calculate scale ---
            
            print(table.width, table.height)
            
            scale_x = config.frame_width/table.width
            scale_y = config.frame_height/table.height
            scale = min([scale_x, scale_y])
            
            print('scale:', scale_x, scale_y, '->', scale)
    
            table = table.scale(scale)
            
            # ---
                    
            self.add(table)
    
            # ---
    
            #self.play(table.animate.scale(2.00))
            #self.play(table.animate.scale(0.25))
    
            #self.wait(3)