def make_movie(self, video_out, source=None): """ Make a movie of either all the clips that have been added, or just a single clip if source is not None :param video_out: Filename of output file. :param source: set to index of a clip to use just that clip, or None to join all clips :return: """ if source is not None: video = create_videoclip(self.frame_sources[source], self.duration[source], self.frame_rate, self.audio_files[source]) else: clips = [ create_videoclip(s, d, self.frame_rate, a) for s, d, a in zip( self.frame_sources, self.duration, self.audio_files) ] video = concatenate_videoclips(clips) # Due to a bug in moviepy 1.0.1, when we write a video out in this mode the audio is not included. # So we write the video and audio out to separate temporary files. # We then use ffmpeg directly to combine the video and audio. temp_video_filename = temp_file( pathlib.Path(video_out).stem + "TEMP.mp4") temp_audio_filename = temp_file( pathlib.Path(video_out).stem + "TEMP.m4a") if not self.audio_files[0]: video.write_videofile(video_out, codec="libx264", fps=self.frame_rate) else: video.write_videofile(temp_video_filename, temp_audiofile=temp_audio_filename, codec="libx264", remove_temp=False, audio_codec="aac", fps=self.frame_rate) command = [ "ffmpeg", "-y", #approve output file overwite "-i", temp_video_filename, "-i", temp_audio_filename, "-c:v", "copy", "-c:a", "copy", "-shortest", "-r", str(self.frame_rate), video_out ] process = sp.Popen(command)
def run_image_test(name, creator): """ Create an image and check it matches the reference image :param name: test name (used as the image file name) :param creator: a function that takes a filepath ans creates an image :return: """ # Create test output folder out_folder_name = 'genpy-test-images' ref_folder_name = 'images' out_folder = temp_file(out_folder_name) Path(out_folder).mkdir(exist_ok=True) # Warn if output file exists, or if reference file doesn't exist out_file = temp_file(out_folder_name, name) ref_file = os.path.join(ref_folder_name, name) if Path(out_file).exists(): print("WARNING temp file {} already exists".format(out_file)) if not Path(ref_file).exists(): print("WARNING reference file {} doesn't exist".format(ref_file)) # Create the test image file creator(out_file) return compare_images(out_file, ref_file)
def test_tempfile_1_name(self): fn = temp_file('testname.png') self.assertEqual('/tmp/testname.png', fn)
from generativepy.geometry import text from generativepy.color import Color from generativepy.utils import temp_file ''' Illustrates simple use of make_image ''' def draw(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, width=5, background=Color(0.4)) ctx.set_source_rgba(*Color(0.5, 0, 0)) ctx.rectangle(0.5, 0.5, 2.5, 1.5) ctx.fill() ctx.set_source_rgba(*Color(0, 0.75, 0, 0.5)) ctx.rectangle(2, 0.25, 2.5, 1) ctx.fill() text(ctx, "simple-make-image", 1, 3, size=0.2, color=Color('cadetblue'), font='Arial') make_image(temp_file("simple-make-image.png"), draw, 500, 400)
from generativepy.bitmap import make_bitmap from generativepy.color import Color from PIL import ImageDraw from generativepy.utils import temp_file ''' Create a simple bitmap image ''' def paint(image, pixel_width, pixel_height, frame_no, frame_count): draw = ImageDraw.Draw(image) draw.rectangle((60, 10, 300, 150), fill=Color("tomato").as_rgbstr()) make_bitmap(temp_file("simple-make-bitmap.png"), paint, 500, 300)
def test_tempfile_2_name(self): fn = temp_file('abc', 'testname.png') self.assertEqual('/tmp/abc/testname.png', fn)
from generativepy.bitmap import make_bitmap_frames from generativepy.movie import save_frames from generativepy.color import Color from PIL import ImageDraw from generativepy.utils import temp_file ''' Create a simple bitmap image ''' def paint(image, pixel_width, pixel_height, frame_no, frame_count): draw = ImageDraw.Draw(image) draw.rectangle((60, 10 + frame_no * 30, 300, 150 + frame_no * 30), fill=Color("tomato").as_rgbstr()) frames = make_bitmap_frames(paint, 500, 300, 4) save_frames(temp_file("simple-make-bitmap-frames.png"), frames)
def colorise(counts): counts = np.reshape(counts, (counts.shape[0], counts.shape[1])) power_counts = np.power(counts, 0.25) maxcount = np.max(power_counts) normalised_counts = (power_counts * 1023 / max(maxcount, 1)).astype( np.uint32) colormap = make_npcolormap(1024, [ Color('black'), Color('red'), Color('orange'), Color('yellow'), Color('white') ]) outarray = np.zeros((counts.shape[0], counts.shape[1], 3), dtype=np.uint8) apply_npcolormap(outarray, normalised_counts, colormap) return outarray data = make_nparray_data(paint, 600, 600, channels=1) filename = temp_file('kings-dream.dat') save_nparray(filename, data) data = load_nparray(filename) frame = colorise(data) save_nparray_image('kings-dream.png', frame)
x, y = scaler.device_to_user(px, py) count = calc(x, y) image[py, px] = count def colorise(counts): counts = np.reshape(counts, (counts.shape[0], counts.shape[1])) colormap = make_npcolormap(MAX_COUNT + 1, [ Color('black'), Color('red'), Color('orange'), Color('yellow'), Color('white') ], [16, 8, 32, 128]) outarray = np.zeros((counts.shape[0], counts.shape[1], 3), dtype=np.uint8) apply_npcolormap(outarray, counts, colormap) return outarray data = make_nparray_data(paint, 600, 600, channels=1) filename = temp_file('tinkerbell.dat') save_nparray(filename, data) data = load_nparray(filename) frame = colorise(data) save_nparray_image('burning-ship-zoom.png', frame)
from generativepy.geometry import Circle, Rectangle from generativepy.color import Color from generativepy.drawing import setup, make_image_frames from generativepy.movie import MovieBuilder from generativepy.tween import TweenVector from generativepy.utils import temp_file # Create a movie with two scenes and audio files WIDTH = 200 # Nominal width, used for all drawing HEIGHT = 300 # Nominal width, used for all drawing OUTPUTWIDTH = 200 # Final video width, could be made larger for 4k output OUTPUTHEIGHT = 300 # Final video height, could be made larger for 4k output FRATE = 10 VIDEONAME = temp_file("movie-audio.mp4") def t2f(t): # Convert a time in seconds to a number of frames return int(t * FRATE) def scene1(duration): position = TweenVector((0, 0)).to((200, 300), t2f(duration)) def draw(ctx, pixel_width, pixel_height, fn, frame_count): setup(ctx, pixel_width, pixel_height, width=OUTPUTWIDTH, height=OUTPUTHEIGHT,
def colorise(counts): counts = np.reshape(counts, (counts.shape[0], counts.shape[1])) colormap = make_npcolormap( int(np.max(counts)) + 1, [ Color('black'), Color('cadetblue'), Color('yellow'), Color('white'), Color('white') ], [50, 100, 100, 102400]) outarray = np.zeros((counts.shape[0], counts.shape[1], 3), dtype=np.uint8) apply_npcolormap(outarray, counts, colormap) return outarray filename = temp_file('popcorn.dat') data = make_nparray_data(paint, WIDTH, WIDTH, channels=1) save_nparray(filename, data) data = load_nparray(filename) print_stats(data) print_histogram(data) frame = colorise(data) save_nparray_image('popcorn.png', frame)
from generativepy.nparray import make_nparray_frame, save_nparray, load_nparray from generativepy.utils import temp_file from generativepy.movie import save_frame ''' saving and loading nparray example ''' def paint(array, pixel_width, pixel_height, frame_no, frame_count): array[10:150, 60:300] = [255, 255, 0] frame = make_nparray_frame(paint, 500, 300) save_nparray(temp_file("saved-nparray.dat"), frame) frame2 = load_nparray(temp_file("saved-nparray.dat")) save_frame(temp_file("save-reload-nparray.png"), frame2)
from generativepy.drawing import make_images, setup from generativepy.geometry import text from generativepy.color import Color from generativepy.utils import temp_file ''' Illustrates simple use of make_images ''' def draw(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, width=5, background=Color(0.4)) ctx.set_source_rgba(*Color(0.5, 0, 0)) ctx.rectangle(0.5, 0.5, 2.5, 1.5) ctx.fill() ctx.set_source_rgba(*Color(0, 0.75, 0, 0.5)) ctx.rectangle(2, 0.25, 2.5, 1) ctx.fill() text(ctx, "simple-make-images {} {}".format(frame_no, frame_count), 1, 3, size=0.2, color=Color('cadetblue'), font='Arial') make_images(temp_file("simple-make-images.png"), draw, 500, 400, 3)
outarray = np.zeros((counts.shape[0], counts.shape[1], 3), dtype=np.uint8) apply_npcolormap(outarray, normalised_counts, colormap) return outarray def paint(image, pixel_width, pixel_height, frame_no, frame_count): scaler = Scaler(pixel_width, pixel_height, width=300, startx=-150, starty=-150) x = -1 y = 0 for i in range(MAX_COUNT): x, y = y - math.sqrt(abs(B * x - C)) * sign(x), A - x px, py = scaler.user_to_device(x, y) if 0 <= px < pixel_width and 0 <= py < pixel_height: image[py, px] += 1 filename = temp_file('hopalong-variant.dat') data = make_nparray_data(paint, 600, 600, channels=1) save_nparray(filename, data) data = load_nparray(filename) frame = colorise(data) save_nparray_image('hopalong-variant.png', frame)
from generativepy.nparray import make_nparrays from generativepy.utils import temp_file ''' make_nparrays example ''' def paint(array, pixel_width, pixel_height, frame_no, frame_count): array[10 + frame_no * 30:150 + frame_no * 30, 60:300] = [255, 128, 0] make_nparrays(temp_file("simple-make-nparrays.png"), paint, 500, 400, 4)
from generativepy.color import Color from generativepy.utils import temp_file ''' Illustrates simple use of make_image_frame ''' def draw(ctx, pixel_width, pixel_height, frame_no, frame_count): setup(ctx, pixel_width, pixel_height, width=5, background=Color(0.4)) ctx.set_source_rgba(*Color(0.5, 0, 0)) ctx.rectangle(0.5, 0.5, 2.5, 1.5) ctx.fill() ctx.set_source_rgba(*Color(0, 0.75, 0, 0.5)) ctx.rectangle(2, 0.25, 2.5, 1) ctx.fill() text(ctx, "simple-make-image-frame", 1, 3, size=0.2, color=Color('cadetblue'), font='Arial') frame = make_image_frame(draw, 500, 400) save_frame(temp_file("simple-make-image-frame.png"), frame)
from generativepy.nparray import make_nparray_frame from generativepy.movie import save_frame from generativepy.utils import temp_file ''' make_nparray_frame example ''' def paint(array, pixel_width, pixel_height, frame_no, frame_count): array[10:150, 60:300] = [255, 128, 0] frame = make_nparray_frame(paint, 500, 300) save_frame(temp_file("simple-make-nparray-frame.png"), frame)
from generativepy.nparray import make_nparray from generativepy.utils import temp_file ''' make_nparray example ''' def paint(array, pixel_width, pixel_height, frame_no, frame_count): array[10:150, 60:300] = [255, 128, 0] make_nparray(temp_file("simple-make-nparray.png"), paint, 500, 300)
outarray = np.zeros((counts.shape[0], counts.shape[1], 3), dtype=np.uint8) apply_npcolormap(outarray, normalised_counts, colormap) return outarray def paint(image, pixel_width, pixel_height, frame_no, frame_count): scaler = Scaler(pixel_width, pixel_height, width=1000, startx=-500, starty=-500) x = -1 y = 0 for i in range(MAX_COUNT): x, y = y - math.sqrt(abs(B * x - C)) * sign(x), A - x px, py = scaler.user_to_device(x, y) if 0 <= px < pixel_width and 0 <= py < pixel_height: image[py, px] += 1 filename = temp_file('hopalong.dat') data = make_nparray_data(paint, 600, 600, channels=1) save_nparray(filename, data) data = load_nparray(filename) frame = colorise(data) save_nparray_image('hopalong.png', frame)
image[py, px] = MAX_COUNT for i in range(MAX_COUNT): z = iterate(z) if converged(z) > 0: image[py, px] = i break def colorise(counts): counts = np.reshape(counts, (counts.shape[0], counts.shape[1])) max_count = int(np.max(counts)) colormap = make_npcolormap( max_count + 1, [Color(0), Color('darkblue'), Color('yellow'), Color(1)], [0.5, 2, 7]) outarray = np.zeros((counts.shape[0], counts.shape[1], 3), dtype=np.uint8) apply_npcolormap(outarray, counts, colormap) return outarray data = make_nparray_data(paint, 600, 600, channels=1) filename = temp_file('newton-cube-time.dat') save_nparray(filename, data) data = load_nparray(filename) frame = colorise(data) save_nparray_image('newton-cube-time.png', frame)
x, y = scaler.device_to_user(px, py) count = calc(x, y) image[py, px] = count def colorise(counts): counts = np.reshape(counts, (counts.shape[0], counts.shape[1])) colormap = make_npcolormap(MAX_COUNT + 1, [ Color('black'), Color('red'), Color('orange'), Color('yellow'), Color('white') ], [16, 8, 32, 128]) outarray = np.zeros((counts.shape[0], counts.shape[1], 3), dtype=np.uint8) apply_npcolormap(outarray, counts, colormap) return outarray data = make_nparray_data(paint, 800, 600, channels=1) filename = temp_file('burning-ship.dat') save_nparray(filename, data) data = load_nparray(filename) frame = colorise(data) save_nparray_image('burning-ship.png', frame)