Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: 'float' object cannot be interpreted as an integer #18606

Closed
ravikantmaurya opened this issue Mar 12, 2021 · 3 comments
Closed

TypeError: 'float' object cannot be interpreted as an integer #18606

ravikantmaurya opened this issue Mar 12, 2021 · 3 comments

Comments

@ravikantmaurya
Copy link

Below code will display the stars wars intro characters flowing from down to top disappearing in the galaxy with stars in background.

Reproducing code example:

"""
Description of the video:
Mimic of Star Wars' opening title. A text with a (false)
perspective effect goes towards the end of space, on a
background made of stars. Slight fading effect on the text.

"""

import numpy as np

from moviepy.editor import *
from moviepy.video.tools.drawing import color_gradient
from skimage import transform as tf

# RESOLUTION

w = 720
h = w*9/16 # 16/9 screen
moviesize = w,h



# THE RAW TEXT
txt = "\n".join([
"A long time ago, in a faraway galaxy,",
"there lived a prince and a princess",
"who had never seen the stars, for they",
"lived deep underground.",
"",
"Many years before, the prince's",
"grandfather had ventured out to the",
"surface and had been burnt to ashes by",
"solar winds.",
"",
"One day, as the princess was coding",
"and the prince was shopping online, a",
"meteor landed just a few megameters",
"from the couple's flat."
])


# Add blanks
txt = 10*"\n" +txt + 10*"\n"


# CREATE THE TEXT IMAGE


clip_txt = TextClip(txt,color='white', align='West',fontsize=25,
                    font='Xolonium-Bold', method='label')


# SCROLL THE TEXT IMAGE BY CROPPING A MOVING AREA

txt_speed = 27
fl = lambda gf,t : gf(t)[int(txt_speed*t):int(txt_speed*t)+int(h),:]
#print(fl)
moving_txt= clip_txt.fl(fl, apply_to=['mask'])


# ADD A VANISHING EFFECT ON THE TEXT WITH A GRADIENT MASK

grad = color_gradient(moving_txt.size,p1=(0,2*h/3),
                p2=(0,h/4),col1=0.0,col2=1.0)
gradmask = ImageClip(grad,ismask=True)
fl = lambda pic : np.minimum(pic,gradmask.img)
moving_txt.mask = moving_txt.mask.fl_image(fl)


# WARP THE TEXT INTO A TRAPEZOID (PERSPECTIVE EFFECT)

def trapzWarp(pic,cx,cy,ismask=False):
    """ Complicated function (will be latex packaged as a fx) """
    Y,X = pic.shape[:2]
    src = np.array([[0,0],[X,0],[X,Y],[0,Y]])
    dst = np.array([[cx*X,cy*Y],[(1-cx)*X,cy*Y],[X,Y],[0,Y]])
    tform = tf.ProjectiveTransform()
    tform.estimate(src,dst)
    im = tf.warp(pic, tform.inverse, output_shape=(Y,X))
    return im if ismask else (im*255).astype('uint8')

fl_im = lambda pic : trapzWarp(pic,0.2,0.3)
fl_mask = lambda pic : trapzWarp(pic,0.2,0.3, ismask=True)
warped_txt= moving_txt.fl_image(fl_im)
warped_txt.mask = warped_txt.mask.fl_image(fl_mask)


# BACKGROUND IMAGE, DARKENED AT 60%

stars = ImageClip('./stars.jpg')
stars_darkened = stars.fl_image(lambda pic: (0.6*pic).astype('int16'))


# COMPOSE THE MOVIE

final = CompositeVideoClip([
         stars_darkened,
         warped_txt.set_pos(('center','bottom'))],
         size = moviesize)


# WRITE TO A FILE

final.set_duration(8).write_videofile("starworms.avi", fps=5)

# This script is heavy (30s of computations to render 8s of video)



"""=====================================================================

    CODE FOR THE VIDEO TUTORIAL

  We will now code the video tutorial for this video.
  When you think about it, it is a code for a video explaining how to
  make another video using some code (this is so meta !).
  This code uses the variables of the previous code (it should be placed
  after that previous code to work).

====================================================================="""



def annotate(clip,txt,txt_color='white',bg_color=(0,0,255)):
    """ Writes a text at the bottom of the clip. """
    
    txtclip = TextClip(txt, fontsize=20, font='Ubuntu-bold',
                       color=txt_color)
                       
    txtclip = txtclip.on_color((clip.w,txtclip.h+6), color=(0,0,255),
                        pos=(6,'center'))
                        
    cvc =  CompositeVideoClip([clip , txtclip.set_pos((0,'bottom'))])
    
    return cvc.set_duration(clip.duration)


def resizeCenter(clip):
    return clip.resize( height=h).set_pos('center')

    
def composeCenter(clip):
    return CompositeVideoClip([clip.set_pos('center')],size=moviesize)



annotated_clips = [ annotate(clip,text) for clip,text in [
               
(composeCenter(resizeCenter(stars)).subclip(0,3),
    "This is a public domain picture of stars"),

(CompositeVideoClip([stars],moviesize).subclip(0,3),
    "We only keep one part."),

(CompositeVideoClip([stars_darkened],moviesize).subclip(0,3),
    "We darken it a little."),

(composeCenter(resizeCenter(clip_txt)).subclip(0,3),
    "We generate a text image."),

(composeCenter(moving_txt.set_mask(None)).subclip(6,9),
    "We scroll the text by cropping a moving region of it."),

(composeCenter(gradmask.to_RGB()).subclip(0,2),
    "We add this mask to the clip."),

(composeCenter(moving_txt).subclip(6,9),
    "Here is the result"),

(composeCenter(warped_txt).subclip(6,9),
    "We now warp this clip in a trapezoid."),

(final.subclip(6,9),
    "We finally superimpose with the stars.")
]]

# Concatenate and write to a file

concatenate_videoclips(annotated_clips).write_videofile('tutorial.avi', fps=5)

Error message:

Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\RaviPhone Data\PYTHONBOOTCAMP\PracticeCode\StarWars\starwarsdarktheme.py", line 96, in
final = CompositeVideoClip([
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\video\compositing\CompositeVideoClip.py", line 79, in init
self.bg = ColorClip(size, color=self.bg_color)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\video\VideoClip.py", line 1014, in init
ImageClip.init(self, np.tile(color, w * h).reshape(shape),
File "<array_function internals>", line 5, in tile
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\numpy\lib\shape_base.py", line 1260, in tile
return c.reshape(shape_out)
TypeError: 'float' object cannot be interpreted as an integer

NumPy/Python version information:

Numpy Version : 1.20.1
Python Version : 3.9.2

@rossbar
Copy link
Contributor

rossbar commented Mar 12, 2021

From the traceback it looks like the problem is in moviepy, you may want to report there instead.

@seberg
Copy link
Member

seberg commented Mar 12, 2021

If you follow the traceback it goes to your code and moviesize, if you look at that definition, it looks wrong to me. So I think moviey is not at fault, the best moviepy could to is trying to improve upon the NumPy error message (or sanitize inputs to be less strict then NumPy).

Thanks for being willing to open a NumPy issue when you suspect one, but this turned out not an issue but a usage question, so going to close. Feel free to continue commenting, but other places are better for help with your code.

@seberg seberg closed this as completed Mar 12, 2021
@seberg
Copy link
Member

seberg commented Mar 12, 2021

PS: If it surprises you that this script might have worked a long time ago: NumPy was not modified in a very long time even before dropping Python2. But it would run fine on Python 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants