Check out Xnconvert, itβs completely free and was made for things like this. You can do a bunch of different actions on a set of images and specify where and how to save the output files.
Python
Welcome to the Python community on the programming.dev Lemmy instance!
π Events
Past
November 2023
- PyCon Ireland 2023, 11-12th
- PyData Tel Aviv 2023 14th
October 2023
- PyConES Canarias 2023, 6-8th
- DjangoCon US 2023, 16-20th (!django π¬)
July 2023
- PyDelhi Meetup, 2nd
- PyCon Israel, 4-5th
- DFW Pythoneers, 6th
- Django Girls Abraka, 6-7th
- SciPy 2023 10-16th, Austin
- IndyPy, 11th
- Leipzig Python User Group, 11th
- Austin Python, 12th
- EuroPython 2023, 17-23rd
- Austin Python: Evening of Coding, 18th
- PyHEP.dev 2023 - "Python in HEP" Developer's Workshop, 25th
August 2023
- PyLadies Dublin, 15th
- EuroSciPy 2023, 14-18th
September 2023
- PyData Amsterdam, 14-16th
- PyCon UK, 22nd - 25th
π Python project:
- Python
- Documentation
- News & Blog
- Python Planet blog aggregator
π Python Community:
- #python IRC for general questions
- #python-dev IRC for CPython developers
- PySlackers Slack channel
- Python Discord server
- Python Weekly newsletters
- Mailing lists
- Forum
β¨ Python Ecosystem:
π Fediverse
Communities
- #python on Mastodon
- c/django on programming.dev
- c/pythorhead on lemmy.dbzer0.com
Projects
- PythΓΆrhead: a Python library for interacting with Lemmy
- Plemmy: a Python package for accessing the Lemmy API
- pylemmy pylemmy enables simple access to Lemmy's API with Python
- mastodon.py, a Python wrapper for the Mastodon API
Feeds
Open GIMP
Go to "File" > "Open" and select your GIF.
Select the "Crop Tool" from the toolbox.
Adjust the crop area to your desired size.
Click the "Crop" button.
Go to "File" > "Export As" and choose GIF as the format. Save your cropped GIF.
FFmpeg
Use the following command to crop the GIF, replacing input.gif with your GIF's filename and specifying the crop dimensions:
ffmpeg -i input.gif -vf "crop=w:h:x:y" output.gif
Let me clarify, there are 3 THOUSAND GIFs that need to be cropped
for i in $( ls *.gif ); do fmpeg -i $i -vf "crop=w:h:x:y" cropped_$i; done
This runs on Linux/Mac or on WSL on windows Make sure there are no spaces in the filenames.
If they're all the same size and need cropped to the same size you could drop them all in one folder and run the ffmpeg command with *.gif. That should do all of them.
You'd have to find a way to automate the output though.
As far as i know, for pillow to do what you want you would need to
- Take a gif and split it in to frames
- Edit each frame individually
- Put frames back together as a gif
- Repeat for every gif
It can be done automagically like you want, but if you're not interested in learning python maybe the XnConvert or ffmpeg comment above might be the way to go for ease of use.
As others have suggested, ffmpeg is a great cli tool. If you aren't comfortable with the terminal you can do it via python like this:
import os
import sys
import subprocess
def crop_media(file_name: str, w: int, h: int, x: int, y: int, new_dir: str) -> None:
try:
subprocess.run(f'ffmpeg -i "{file_name}" -vf "crop={w}:{h}:{x}:{y}" temp.gif -y',
shell=True, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
os.rename('temp.gif', os.path.join(new_dir, file_name))
# Print the error and continue with other gifs, remove try block if you want a complete stop
except subprocess.CalledProcessError as e:
print(e)
except KeyboardInterrupt:
print('KeyboardInterrupt, cleaning up files...')
os.remove('temp.gif')
sys.exit(0)
def crop_directory(directory: str, w: int, h: int, x: int, y: int, new_dir: str) -> None:
for root, _, files in directory:
for file in files:
if not file.endswith('.gif'):
continue
if os.path.isfile(os.path.join(new_dir, file)):
print(f'{file} already exists in {new_dir}, skipping...')
continue
file_path = os.path.normpath(os.path.join(root, file))
crop_media(file_path, w, h, x, y, new_dir)
if __name__ == '__main__':
width = 0
height = 0
x_offset = 0
y_offset = 0
gif_directory = ''
new_directory = ''
crop_directory(gif_directory, width, height, x_offset, y_offset, new_directory)
This should go through every file in the directory and subdirectories and call the ffmpeg command on each .gif. With new_directory
you can set a directory to store every cropped .gif. The ffmpeg command is based on johnpiers suggestion.
The script assumes unique filenames for each gif and should work with spaces in the filenames. If they aren't unique, you can just remove the new_directory
part, but you will then lose the original gif that you cropped.