Is there a simple way to get a 3d effect similar to flipping of pages in a book using python

Viewed 324

I am working on a video processing project and I need to add a 3d effect similar to the flipping of pages in a book using python with multiple photos. I am using moviepy and have gone through Rotate an image around its x-axis. However, I am unable to stitch multiple(say 2) photos using the above solution. only one photo is rotating and the other photo comes in between as slide. Also, its processing takes too long on my mac. Is there any other solution to the problem or any suggestion in my code to fix it?

from moviepy.editor import concatenate, ImageClip, VideoClip
from vapory import *
img_path1 = 'IMG_4936.JPG' 
img_path2 = 'IMG_4935.JPG'
list = [img_path2,img_path1]
img_clip = ImageClip(img_path1)
W, H = img_clip.w, img_clip.h
AR = 1.0*W/H
screensize = (W,H)

# Set rotation rate by defining the period (in seconds) for 360 deg. revolution
t_rev = 2.0
total_duration = 9.0
t_half = 1.0  # The time required for a half revolution
t_still = 1.0 # How long (in seconds) to hold the half rotated image still

# Static POV-Ray objects
cam = Camera('location', [ 0,  0, -1],
             'look_at',  [ 0,  0,  0])
light = LightSource([0, 0, -1]) # Light at camera location
bg = Background('color', [0, 0, 0]) # Black background
slides =[]
f_slides=[]

count=-1
for i in list:
    count += 1
    print("path",i)
    start_time = time.time()
    print("before scene: ", count)

    def scene(t):
        print("inside scene: ",count)
        """ Returns the scene at time 't' (in seconds) """
        s = Scene(camera = cam, objects = [light, bg])
    # Add POV-Ray box with image textured on it
    s = s.add_objects([
          Box([0, 0, 0],
              [W, H, 0],
              Texture(Pigment(ImageMap('"{}"'.format(i), 'once')),
                      Finish('ambient', 1.0)),
              'translate', [-0.5, -0.5, 0],
              'scale', [AR, 1, 0],
              'rotate', [0, (360/t_rev)*t, 0])]) # Can change axis of rotation here
    print("format path: ", i)
    return s

    def make_frame(t):
    return scene(t).render(width=W, height=H, antialiasing=0.1)


    still_1 = VideoClip(make_frame).to_ImageClip(t=count*2*t_rev).set_duration(t_still)
    half_1  = VideoClip(make_frame).subclip(count*2*t_rev, count*2*t_rev+t_half)
    still_2 = VideoClip(make_frame).to_ImageClip(t=count*2*t_rev+t_half).set_duration(t_still)
    half_2  = VideoClip(make_frame).subclip(count*2*t_rev+t_half, count*2*t_rev+t_rev)

    final_clip = concatenate([still_1, half_1, still_2, half_2 ])
    f_slides.append(final_clip)



final_clip_f = concatenate_videoclips([j for j in f_slides],method="compose").set_duration(total_duration)
final_clip_f.write_videofile("pic_rot.mp4", fps=12, ffmpeg_params=['-movflags', '+faststart'])
end_time = time.time()
print("Total time for creating video = {}".format(end_time - start_time))

Also, the make_frame function and scene function are being called numerously. I don’t understand this repetition.

0 Answers
Related