kvos / CoastSat

Global shoreline mapping tool from satellite imagery
http://coastsat.space
GNU General Public License v3.0
697 stars 252 forks source link

Makeanimationupdate #523

Open thekester opened 4 months ago

thekester commented 4 months ago

Hello, this pull request addresses issue #522 by adding new functionality to create and update animations from a series of images. It includes the following key changes:

    i)  Function to Resize Images:
        Added `resize_image_to_fixed_size(image, size=(2864, 1440))` function to ensure all images are resized to a consistent size for the animation.

    ii) Function to Load and Resize Images:
        Introduced` load_and_resize_images(image_folder, size=(2864, 1440))` to load images from a specified folder and resize them.

   iii) Function to Create Animation:
        Developed create_animation(image_folder, output_file, fps=4, size=(2864, 1440), probesize=5000000) to compile images into a video file.The function checks for consistent image sizes and raises an error if discrepancies are found.

    iv) Convenience Function to Make MP4 Animation:
        Added make_animation_mp4(filepath_images, fps, fn_out) to simplify the creation of MP4 animations.

Changes made in SDS_tools and include 2 libraries import cv2 from PIL import Image to implement them, replace pip install pyqt5 imageio-ffmpeg with pip install pyqt5 imageio-ffmpeg opencv-python Pillow

kvos commented 3 months ago

hi @thekester , sorry I took 3 weeks to review this. I like the PR so that we don't get that warning anymore but I prefer not to add another module like opencv as there are now other toolkits that share dependencies with coastsat (like coastseg). So would there be a way to avoid the warning without bringing in new packages?

thekester commented 3 months ago

Since Pillowis already a dependency of matplotlib (matplotlib) and we have imageio, there’s definitely a way to avoid the warning without implementing OpenCV. I’m working on a solution using these existing libraries, and I’ll add the commit to the PR shortly (add on (https://github.com/kvos/CoastSat/pull/523/commits/ef71e417d234f476094cd73de498cc8f16795289 )

kvos commented 3 months ago

thanks! will test it

kvos commented 3 months ago

@thekester , was wondering why we need to resize the image to 2864, 1440? this may create animations that are quite large no?

thekester commented 3 months ago

Please feel free to choose the most appropriate size; the example providedsize=(2864, 1440) is just a suggestion. The goal is to ensure all images are the same size before creating the animation, thereby avoiding the IMAGEIO FFMPEG WRITER WARNING.

kvos commented 2 months ago

@thekester , I'm not convince about the resizing as every ROI has a different aspect ratio, some beaches are E or W facing while other are N or S facing, then if you resize all the images to a fixed size it will not work. I am happy to live with the warning as it seems to create good quality clips anyways. But if you want to resize, I suggest you find the most common size in the image folder and use that size for resizing all the images. It can happen that some satellite images are clipped, hence the difference in size that ffpmeg doesn't like. Something like this:

list_files = sorted(os.listdir(filepath_images))
sizes = [Image.open(os.path.join(filepath_images,_)).size for _ in list_files]
unique_sizes = set(sizes)
# select most common size
thekester commented 2 months ago

Thank you for your feedback, @kvos. I hadn't considered the issue of different aspect ratios due to the varying orientations of the beaches—this is a great point. I'll look into a method to automatically detect the aspect ratio of each image, identifying whether the beaches are oriented East-West or North-South. Then, I can adjust the resizing process to maintain the original proportions of the images while still avoiding the IMAGEIO FFMPEG warnings.

I'll test this approach and add a commit if it works.