From 987c849fb9be88a355281931978180e8d862b1aa Mon Sep 17 00:00:00 2001 From: "vahid K. nejad" Date: Tue, 11 Apr 2023 07:05:09 +0400 Subject: [PATCH] iz_helpers added --- iz_helpers/image.py | 33 +++++++++++++++++++++++++++++++++ iz_helpers/video.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 iz_helpers/image.py create mode 100644 iz_helpers/video.py diff --git a/iz_helpers/image.py b/iz_helpers/image.py new file mode 100644 index 0000000..8046829 --- /dev/null +++ b/iz_helpers/image.py @@ -0,0 +1,33 @@ +import requests +from PIL import Image +import numpy as np + + +def shrink_and_paste_on_blank(current_image, mask_width): + """ + Decreases size of current_image by mask_width pixels from each side, + then adds a mask_width width transparent frame, + so that the image the function returns is the same size as the input. + :param current_image: input image to transform + :param mask_width: width in pixels to shrink from each side + """ + + height = current_image.height + width = current_image.width + + # shrink down by mask_width + prev_image = current_image.resize( + (height-2*mask_width, width-2*mask_width)) + prev_image = prev_image.convert("RGBA") + prev_image = np.array(prev_image) + + # create blank non-transparent image + blank_image = np.array(current_image.convert("RGBA"))*0 + blank_image[:, :, 3] = 1 + + # paste shrinked onto blank + blank_image[mask_width:height-mask_width, + mask_width:width-mask_width, :] = prev_image + prev_image = Image.fromarray(blank_image) + + return prev_image diff --git a/iz_helpers/video.py b/iz_helpers/video.py new file mode 100644 index 0000000..afffe1a --- /dev/null +++ b/iz_helpers/video.py @@ -0,0 +1,43 @@ +import numpy as np +import imageio +from PIL import Image + +def write_video(file_path, frames, fps, reversed=True, start_frame_dupe_amount=15, last_frame_dupe_amount=30): + """ + Writes frames to an mp4 video file + :param file_path: Path to output video, must end with .mp4 + :param frames: List of PIL.Image objects + :param fps: Desired frame rate + :param reversed: if order of images to be reversed (default = True) + """ + if reversed == True: + frames = frames[::-1] + + # Get dimensions of the frames + w, h = frames[0].size + + # Create an imageio video writer + writer = imageio.get_writer(file_path, fps=fps) + + # Duplicate the start and end frames + start_frames = [frames[0]] * start_frame_dupe_amount + end_frames = [frames[-1]] * last_frame_dupe_amount + + # Write the duplicated frames to the video writer + for frame in start_frames: + # Convert PIL image to numpy array + np_frame = np.array(frame) + writer.append_data(np_frame) + + # Write the frames to the video writer + for frame in frames: + np_frame = np.array(frame) + writer.append_data(np_frame) + + # Write the duplicated frames to the video writer + for frame in end_frames: + np_frame = np.array(frame) + writer.append_data(np_frame) + + # Close the video writer + writer.close() \ No newline at end of file