Example #1
0
def generate_video_from_list(image_list, save_path, framerate=30, downsample=1, warning=True, debug=True):
	'''
	create video from a list of images with a framerate
	note that: the height and widht of the images should be a multiple of 2

	parameters:
		image_list:			a list of image path
		save_path:			the path to save the video file
		framerate:			fps 
	'''
	if debug: 
		assert islistofstring(image_list), 'the input is not correct'
		assert ispositiveinteger(framerate), 'the framerate is a positive integer'
	mkdir_if_missing(save_path)
	inputdict = {'-r': str(framerate)}
	outputdict = {'-r': str(framerate), '-crf': '18', '-vcodec': 'libx264', '-profile:V': 'high', '-pix_fmt': 'yuv420p'}
	video_writer = FFmpegWriter(save_path, inputdict=inputdict, outputdict=outputdict)
	count = 1
	num_images = len(image_list)
	for image_path in image_list:
		print('processing frame %d/%d' % (count, num_images))
		image = load_image(image_path, resize_factor=downsample, warning=warning, debug=debug)

		# make sure the height and width are multiple of 2
		height, width = image.shape[0], image.shape[1]
		if not (height % 2 == 0 and width % 2 == 0):
			height += height % 2
			width += width % 2
			image = image_resize(image, target_size=[height, width], warning=warning, debug=debug)

		video_writer.writeFrame(image)
		count += 1

	video_writer.close()
Example #2
0
def load_image(src_path, resize_factor=None, target_size=None, input_angle=0, gray=False, warning=True, debug=True):
    '''
    load an image from given path, with preprocessing of resizing and rotating, output a rgb image

    parameters:
        resize_factor:      a scalar
        target_size:        a list or tuple or numpy array with 2 elements, representing height and width
        input_angle:        a scalar, counterclockwise rotation in degree

    output:
        np_image:           an uint8 rgb numpy image
    '''
    src_path = safe_path(src_path, warning=warning, debug=debug)
    if debug: assert is_path_exists(src_path), 'image path is not correct at %s' % src_path
    if resize_factor is None and target_size is None: resize_factor = 1.0           # default not to have resizing

    with open(src_path, 'rb') as f:
        with Image.open(f) as img:
            if gray: img = img.convert('L')
            else: 
                try:
                    img = img.convert('RGB')
                except IOError:
                    print(src_path)
                    zxc
            np_image = image_rotate(img, input_angle=input_angle, warning=warning, debug=debug)
            np_image = image_resize(np_image, resize_factor=resize_factor, target_size=target_size, warning=warning, debug=debug)
    return np_image
Example #3
0
def save_image(input_image, save_path, resize_factor=None, target_size=None, input_angle=0, warning=True, debug=True):
    '''
    load an image to a given path, with preprocessing of resizing and rotating

    parameters:
        resize_factor:      a scalar
        target_size:        a list of tuple or numpy array with 2 elements, representing height and width
        input_angle:        a scalar, counterclockwise rotation in degree
    '''
    save_path = safe_path(save_path, warning=warning, debug=debug); mkdir_if_missing(save_path)
    if debug: is_path_exists_or_creatable(save_path), 'the path is not good to save'
    np_image, _ = safe_image(input_image, warning=warning, debug=debug)
    if resize_factor is None and target_size is None: resize_factor = 1.0           # default not to have resizing

    # preprocessing the image before saving
    np_image = image_rotate(np_image, input_angle=input_angle, warning=warning, debug=debug)
    np_image = image_resize(np_image, resize_factor=resize_factor, target_size=target_size, warning=warning, debug=debug)
    
    # saving
    pil_image = Image.fromarray(np_image)
    pil_image.save(save_path)