From a50e538c830c8b7fc22885f9273dc976f3413e0b Mon Sep 17 00:00:00 2001 From: Bitdeli Chef Date: Mon, 20 Jul 2015 15:20:12 +0000 Subject: [PATCH 1/2] Add a Bitdeli badge to README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index f5bab0e..d198d6b 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,7 @@ python-scripts ============== useful python script. + + +[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/soarpenguin/python-scripts/trend.png)](https://bitdeli.com/free "Bitdeli Badge") + From d3a3826995d44456a83c07756b1160e252d53741 Mon Sep 17 00:00:00 2001 From: soarpenguin Date: Wed, 21 Jun 2023 10:37:18 +0800 Subject: [PATCH 2/2] add gif frame edit tools. --- remove_black_frames.py | 37 +++++++++++++++++++++++++++++++++++ remove_frame.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 remove_black_frames.py create mode 100644 remove_frame.py diff --git a/remove_black_frames.py b/remove_black_frames.py new file mode 100644 index 0000000..601474e --- /dev/null +++ b/remove_black_frames.py @@ -0,0 +1,37 @@ +import sys +from PIL import Image, ImageSequence + +def is_black_frame(frame): + # Convert the frame to grayscale + grayscale_frame = frame.convert('L') + + # Check if all pixels in the grayscale frame are black (0) + return all(pixel == 0 for pixel in grayscale_frame.getdata()) + +def remove_black_frames(input_path, output_path): + # Open the GIF image + with Image.open(input_path) as im: + # Create a list to hold the modified frames + modified_frames = [] + + # Iterate over each frame in the GIF + for frame in ImageSequence.Iterator(im): + if not is_black_frame(frame): + modified_frames.append(frame.copy()) + + # Save the modified frames as a GIF + modified_frames[0].save(output_path, save_all=True, append_images=modified_frames[1:], loop=0) + +if __name__ == '__main__': + # Check if the required command-line arguments are provided + if len(sys.argv) != 3: + print('Usage: python remove_black_frames.py ') + sys.exit(1) + + # Extract command-line arguments + input_gif = sys.argv[1] + output_gif = sys.argv[2] + + # Remove black frames and save the modified GIF + remove_black_frames(input_gif, output_gif) + diff --git a/remove_frame.py b/remove_frame.py new file mode 100644 index 0000000..0009d53 --- /dev/null +++ b/remove_frame.py @@ -0,0 +1,44 @@ +import sys +import re +from PIL import Image, ImageSequence + +def parse_frame_indices(frames): + parsed_indices = [] + for frame in frames.split(','): + match = re.match(r'^(\d+)-(\d+)$', frame) + if match: + start, end = map(int, match.groups()) + parsed_indices.extend(range(start, end + 1)) + else: + parsed_indices.append(int(frame)) + return parsed_indices + +def remove_frames(input_path, output_path, frames_to_remove): + # Open the GIF image + with Image.open(input_path) as im: + # Create a list to hold the modified frames + modified_frames = [] + + # Iterate over each frame in the GIF + for index, frame in enumerate(ImageSequence.Iterator(im)): + # Check if the current frame index is in the frames_to_remove list + if index not in frames_to_remove: + modified_frames.append(frame.copy()) + + # Save the modified frames as a GIF + modified_frames[0].save(output_path, save_all=True, append_images=modified_frames[1:], loop=0) + +if __name__ == '__main__': + # Check if the required command-line arguments are provided + if len(sys.argv) != 4: + print('Usage: python remove_frames.py ') + sys.exit(1) + + # Extract command-line arguments + input_gif = sys.argv[1] + output_gif = sys.argv[2] + frames_to_remove = parse_frame_indices(sys.argv[3]) + + # Remove frames and save the modified GIF + remove_frames(input_gif, output_gif, frames_to_remove) +