2013-06-25 3 views
2

루프를 통해 프레임을 만드는 동안 동영상을 만들려고합니다. 그것은 절약이지만 첫 번째 프레임 (영화로 단편 영화로 재생됩니다!) 나는 여러 가지를 시도하고 내가 뭘 잘못하고 있는지 알 수 없습니다. 감사합니다자이 썬/파이썬으로 동영상 만들기

def synthesiseFrame(folder): 
    folder =r"D:\FOLDER" 
    m=0.5 
    for x in range(1,121): 
    pic=makeEmptyPicture(960,540) 
    for x in range (0,960): 
     for y in range (0,540): 
     r=#some code 
     g=#some code 
     b=#some code 
     color =makeColor (r,g,b) 
     px= getPixel (pic, x, y) 
     setColor(px, color) 
    numStr=str(x) 

    m=m+0.0125 
    if x<10: 
     writePictureTo(pic, folder+"\pic00"+numStr+".png") 
    if x >=10 and x<100: 
     writePictureTo(pic, folder+"\pic0"+numStr+".png") 
    if x>=100: 
     writePictureTo(pic,folder+"\pic"+numStr+".png") 

    return movie 

movie=synthesiseFrame(folder) 
folder =r"D:\FOLDER" 
file=r"D:\FOLDER\pic00.png" 
movie=makeMovieFromInitialFile(file) 
writeQuicktime(movie,"D:\FOLDER\movie.mov", 30) 
playMovie(movie) 
+1

글쎄, 취침 시간. 그러나 그것은 시원했다. +1 재미;) 곧 보자. –

+1

Ouaip, ca marche! A +. 고티에. –

답변

3

JES video functions에서 당신의 코드에서 내 첫눈처럼 나에게 뭔가를 알려줍니다 (완전히 작동하는 예) :

import os 
import random 

def synthesizeFrameAndCreateMovie(folder): 

    # Create an empty movie to receive the frames 
    movie = makeMovie() 

    # Compute & save the frames 
    w = 40 
    h = 25 
    nb_frames = 60  # Will give 60 frames at 30 fps => movie duration : 2 sec. 
    for z in range(0, nb_frames): 
    pic=makeEmptyPicture(w, h) 
    for x in range (0, w): 
     for y in range (0, h): 
     #makeColor() takes red, green, and blue (in that order) between 0 and 255 
     r = random.randint(0, 255) 
     g = random.randint(0, 255) 
     b = random.randint(0, 255) 
     color = makeColor(r,g,b) 
     px = getPixel(pic, x, y) 
     setColor(px, color) 

    # Create one frame and inject in the movie object 
    filename = os.path.join(folder, 'pic%03d.png' % z) 
    writePictureTo(pic, filename) 
    addFrameToMovie(filename, movie) 

    # return the movie 
    return movie 

movie = synthesizeFrameAndCreateMovie("D:\\FOLDER") 
print movie 
#writeQuicktime(movie,"D:\\FOLDER\\movie.mov", 30) 
playMovie(movie) 


출력 (프레임)


...... enter image description here ... enter image description hereenter image description here ... ... ... enter image description hereenter image description here ... enter image description here ... enter image description here ... enter image description here ... enter image description here ... enter image description here ... enter image description here ... enter image description here ......


,363,210

편집 :

더 재미 : 는 ... 선 (코드 촬영 형태 here을) 애니메이션

import os 
import random 

# Draw point, with check if the point is in the image area 
def drawPoint(pic, col, x, y): 
    if (x >= 0) and (x < getWidth(pic)) and (y >= 0) and (y < getHeight(pic)): 
    px = getPixel(pic, x, y) 
    setColor(px, col) 


# Draw line segment, given two points 
# From Bresenham's line algorithm 
# http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm 
def drawLine(pic, col, x0, y0, x1, y1): 

    dx = abs(x1-x0) 
    dy = abs(y1-y0) 
    sx = sy = 0 

    #sx = 1 if x0 < x1 else -1 
    #sy = 1 if y0 < y1 else -1 

    if (x0 < x1): 
    sx = 1 
    else: 
    sx = -1 
    if (y0 < y1): 
    sy = 1 
    else: 
    sy = -1 

    err = dx - dy 

    while (True): 

    drawPoint(pic, col, x0, y0) 

    if (x0 == x1) and (y0 == y1): 
     break 

    e2 = 2 * err 
    if (e2 > -dy): 
     err = err - dy 
     x0 = x0 + sx 

    if (x0 == x1) and (y0 == y1): 
     drawPoint(pic, col, x0, y0) 
     break 

    if (e2 < dx): 
     err = err + dx 
     y0 = y0 + sy 

# Draw infinite line from segment 
def drawInfiniteLine(pic, col, x0, y0, x1, y1): 
    # y = m * x + b 
    m = (y0-y1)/(x0-x1) 
    if (abs(m) > 100.0): 
    m = 100.0 

    # y0 = m * x0 + b => b = y0 - m * x0 
    b = y0 - m * x0 

    x0 = 0 
    y0 = int(m*x0 + b) 
    # get a 2nd point far away from the 1st one 
    x1 = getWidth(pic) 
    y1 = int(m*x1 + b) 

    drawLine(pic, col, x0, y0, x1, y1) 

# Draw infinite line from origin point and angle 
# Angle 'theta' expressed in degres 
def drawInfiniteLineA(pic, col, x, y, theta): 

    # y = m * x + b 
    dx = y * tan(theta * pi/180.0) # (need radians) 
    dy = y 

    if (dx == 0): 
    dx += 0.000000001 # Avoid to divide by zero 

    m = dy/dx 

    # y = m * x + b => b = y - m * x 
    b = y - m * x 

    # get a 2nd point far away from the 1st one 
    x1 = 2 * getWidth(pic) 
    y1 = m*x1 + b 

    drawInfiniteLine(pic, col, x, y, x1, y1) 


def synthesizeFrameAndCreateMovie(folder): 

    # Create an empty movie to receive the frames 
    movie = makeMovie() 

    # Compute & save the frames 
    w = 40 
    h = 25 
    nb_frames = 120  # Will give 120 frames at 30 fps => movie duration : 4 sec. 
    for z in range(0, nb_frames): 
    pic = makeEmptyPicture(w, h) 
    addRectFilled(pic, 0, 0, w-1, h-1) 
    #makeColor() takes red, green, and blue (in that order) between 0 and 255 
    r = random.randint(0, 255) 
    g = random.randint(0, 255) 
    b = random.randint(0, 255) 
    col = makeColor(r,g,b) 
    theta = z * 360/nb_frames 
    if (theta != 180.0) and (theta != 0.0): 
     drawInfiniteLineA(pic, col, w//2, h//2, theta) 

    # Create one frame and inject in the movie object 
    filename = os.path.join(folder, 'pic%03d.png' % z) 
    writePictureTo(pic, filename) 
    addFrameToMovie(filename, movie) 

    # return the movie 
    return movie 

movie = synthesizeFrameAndCreateMovie("/home/FOLDER") 
print movie 
#writeQuicktime(movie,"/home/golgauth/Desktop/FOLDER/movie.mov", 30) 
#writeAVI(movie,"/home/golgauth/Desktop/FOLDER/movie.avi") 
playMovie(movie) 


출력 (프레임) :


...... enter image description here ... enter image description here ... enter image description here ... enter image description here ... enter image description here ... enter image description here ... enter image description here ... enter image description here ... enter image description here ... enter image description here ... enter image description here .. enter image description here ......


+1

'% 03d '은 000, 001, 002, ... 100의 3 자리 숫자로 코딩 된 정수를 의미합니다. –

+0

영화가 아닌 파일로 저장할 수 있는지 알려주세요. –

+0

@Yve 좀 더 흥미로운 예제 인 '회전 선을 사용한 애니메이션'으로 내 대답을 편집했습니다. 그것을 시도하고 싶다면; –

3

코드가 변경되었습니다.

  • * 3 대신 % 03d '% x를 사용했습니다.
  • synthesiseFrame의 루프가 1부터 시작하므로 'pic001.png'를 'pic001.png'로 변경하십시오.
  • '\'-> os.path.join (..); 그렇지 않은 경우 import os을 입력하십시오.

def synthesiseFrame(folder): 
    m = 0.5 
    for frameNumber in range(1,121): 
    pic=makeEmptyPicture(960,540) 
    for x in range (0,960): 
     for y in range (0,540): 
     r = #some code 
     g = #some code 
     b = #some code 
     color =makeColor (r,g,b) 
     px= getPixel (pic, x, y) 
     setColor(px, color) 
    m += 0.0125 
    writePictureTo(pic, os.path.join(folder, 'pic%03d.png' % frameNumber)) # 3 if -> no if 
    return movie 

movie = synthesiseFrame(folder) 
folder = r"D:\FOLDER" 
file = r"D:\FOLDER\pic001.png" # 00 -> 001 
movie=makeMovieFromInitialFile(file) 
writeQuicktime(movie,"D:\FOLDER\movie.mov", 30) 
playMovie(movie) 

편집

  • x (외부 루프) ->frameNumber
+0

또한'synthesiseFrame'에 결코 존재하지 않는 변수'movie'를 반환합니다.이 함수는 동영상을 효과적으로 생성하지 않으면 아무 것도 반환하지 않아야합니다. –

+0

나는 나의 코드를 테스트했다 : 그것은 작동한다. 나는 지금 나의 대답을 업데이트한다. –

+0

내가 알아낼 수없는 마지막 한 가지는 디스크에 저장하는 방법입니다. 하지만 제대로 할 수 있어요. –

관련 문제