From 4306cbf4ee9f901096862ea08ed143320492070b Mon Sep 17 00:00:00 2001 From: thekester Date: Mon, 29 Jul 2024 11:37:10 +0200 Subject: [PATCH 1/3] Add new version of make_animation_mp4 --- coastsat/SDS_tools.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/coastsat/SDS_tools.py b/coastsat/SDS_tools.py index 64c41cf2..dbe598ae 100644 --- a/coastsat/SDS_tools.py +++ b/coastsat/SDS_tools.py @@ -23,6 +23,8 @@ import pyproj import pandas as pd import imageio +import cv2 +from PIL import Image ################################################################################################### # COORDINATES CONVERSION FUNCTIONS @@ -872,6 +874,7 @@ def smallest_rectangle(polygon): # MAKE ANIMATIONS ################################################################################################### +""" def make_animation_mp4(filepath_images, fps, fn_out): "function to create an animation with the saved figures" with imageio.get_writer(fn_out, mode='I', fps=fps) as writer: @@ -881,8 +884,43 @@ def make_animation_mp4(filepath_images, fps, fn_out): for i in range(len(filenames)): image = imageio.imread(os.path.join(filepath_images,filenames[i])) writer.append_data(image) - print('Animation has been generated (using %d frames per second) and saved at %s'%(fps,fn_out)) + print('Animation has been generated (using %d frames per second) and saved at %s'%(fps,fn_out))""" + +# Define functions for resizing and creating animations +def resize_image_to_fixed_size(image, size=(2864, 1440)): + return cv2.resize(image, size) + +def load_and_resize_images(image_folder, size=(2864, 1440)): + images = [] + for filename in sorted(os.listdir(image_folder)): + if filename.endswith(".jpg"): + img_path = os.path.join(image_folder, filename) + image = np.array(Image.open(img_path)) + resized_image = resize_image_to_fixed_size(image, size) + images.append((filename, resized_image)) + return images + +def create_animation(image_folder, output_file, fps=4, size=(2864, 1440), probesize=5000000): + images = load_and_resize_images(image_folder, size) + # Check for different image sizes + sizes = [img[1].shape for img in images] + unique_sizes = set(sizes) + if len(unique_sizes) > 1: + print("Images have different sizes: ", unique_sizes) + for img in images: + print(f"{img[0]}: {img[1].shape}") + raise ValueError("All images in a movie should have same size") + + writer = imageio.get_writer(output_file, fps=fps, ffmpeg_params=['-probesize', str(probesize)]) + for filename, image in images: + writer.append_data(image) + writer.close() + +def make_animation_mp4(filepath_images, fps, fn_out): + create_animation(filepath_images, fn_out, fps) + print('Animation has been generated (using %d frames per second) and saved at %s'%(fps, fn_out)) + ################################################################################################### # VALIDATION ################################################################################################### From edfa4a40e109e8aff2d5f5a4bd264e86b1b53d2e Mon Sep 17 00:00:00 2001 From: thekester Date: Mon, 29 Jul 2024 11:48:47 +0200 Subject: [PATCH 2/3] Add comments on new functions to make mp4 animation --- coastsat/SDS_tools.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/coastsat/SDS_tools.py b/coastsat/SDS_tools.py index dbe598ae..c28aa9aa 100644 --- a/coastsat/SDS_tools.py +++ b/coastsat/SDS_tools.py @@ -888,9 +888,29 @@ def make_animation_mp4(filepath_images, fps, fn_out): # Define functions for resizing and creating animations def resize_image_to_fixed_size(image, size=(2864, 1440)): + """ + Resize an image to a fixed size. + + Args: + image (numpy.ndarray): The image to be resized. + size (tuple): The target size for the image. + + Returns: + numpy.ndarray: The resized image. + """ return cv2.resize(image, size) def load_and_resize_images(image_folder, size=(2864, 1440)): + """ + Load and resize all images in a folder to a fixed size. + + Args: + image_folder (str): Path to the folder containing images. + size (tuple): The target size for the images. + + Returns: + list: A list of tuples, each containing the filename and the resized image. + """ images = [] for filename in sorted(os.listdir(image_folder)): if filename.endswith(".jpg"): @@ -901,6 +921,19 @@ def load_and_resize_images(image_folder, size=(2864, 1440)): return images def create_animation(image_folder, output_file, fps=4, size=(2864, 1440), probesize=5000000): + """ + Create an animation from images in a folder and save it as a video file. + + Args: + image_folder (str): Path to the folder containing images. + output_file (str): Path to the output video file. + fps (int): Frames per second for the video. + size (tuple): The target size for the images. + probesize (int): Probesize parameter for ffmpeg. + + Raises: + ValueError: If images have different sizes. + """ images = load_and_resize_images(image_folder, size) # Check for different image sizes @@ -918,6 +951,14 @@ def create_animation(image_folder, output_file, fps=4, size=(2864, 1440), probes writer.close() def make_animation_mp4(filepath_images, fps, fn_out): + """ + Generate an animation (MP4) from images in a specified folder. + + Args: + filepath_images (str): Path to the folder containing images. + fps (int): Frames per second for the video. + fn_out (str): Path to the output video file. + """ create_animation(filepath_images, fn_out, fps) print('Animation has been generated (using %d frames per second) and saved at %s'%(fps, fn_out)) From ef71e417d234f476094cd73de498cc8f16795289 Mon Sep 17 00:00:00 2001 From: thekester Date: Wed, 21 Aug 2024 16:21:22 +0200 Subject: [PATCH 3/3] Remove opencv dependency --- coastsat/SDS_tools.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/coastsat/SDS_tools.py b/coastsat/SDS_tools.py index c28aa9aa..07bfb7ee 100644 --- a/coastsat/SDS_tools.py +++ b/coastsat/SDS_tools.py @@ -23,7 +23,6 @@ import pyproj import pandas as pd import imageio -import cv2 from PIL import Image ################################################################################################### @@ -898,7 +897,7 @@ def resize_image_to_fixed_size(image, size=(2864, 1440)): Returns: numpy.ndarray: The resized image. """ - return cv2.resize(image, size) + return image.resize(size) def load_and_resize_images(image_folder, size=(2864, 1440)): """ @@ -915,7 +914,7 @@ def load_and_resize_images(image_folder, size=(2864, 1440)): for filename in sorted(os.listdir(image_folder)): if filename.endswith(".jpg"): img_path = os.path.join(image_folder, filename) - image = np.array(Image.open(img_path)) + image = Image.open(img_path) resized_image = resize_image_to_fixed_size(image, size) images.append((filename, resized_image)) return images @@ -937,17 +936,17 @@ def create_animation(image_folder, output_file, fps=4, size=(2864, 1440), probes images = load_and_resize_images(image_folder, size) # Check for different image sizes - sizes = [img[1].shape for img in images] + sizes = [img[1].size for img in images] unique_sizes = set(sizes) if len(unique_sizes) > 1: print("Images have different sizes: ", unique_sizes) for img in images: - print(f"{img[0]}: {img[1].shape}") - raise ValueError("All images in a movie should have same size") - + print(f"{img[0]}: {img[1].size}") + raise ValueError("All images in a movie should have the same size") + writer = imageio.get_writer(output_file, fps=fps, ffmpeg_params=['-probesize', str(probesize)]) for filename, image in images: - writer.append_data(image) + writer.append_data(np.array(image)) writer.close() def make_animation_mp4(filepath_images, fps, fn_out):