Last active
February 8, 2024 23:06
-
-
Save Adrian-2105/76bfd7a3dda9725756ff9ecb4fbf8a9e to your computer and use it in GitHub Desktop.
Crop an image/dir of images from CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import subprocess | |
import sys | |
import argparse | |
parser = argparse.ArgumentParser(description='Crop an image.') | |
parser.add_argument('image', metavar='image', type=str, help='the image to crop') | |
parser.add_argument('left', metavar='left', type=int, help='the left side to crop') | |
parser.add_argument('right', metavar='right', type=int, help='the right side to crop') | |
parser.add_argument('top', metavar='top', type=int, help='the top side to crop') | |
parser.add_argument('bottom', metavar='bottom', type=int, help='the bottom side to crop') | |
parser.add_argument('-o', '--output', metavar='output', type=str, help='the output file') | |
args = parser.parse_args() | |
# check if imagemagick is installed | |
try: | |
subprocess.check_output(["identify", "-version"]) | |
except: | |
print("Please install imagemagick.") | |
sys.exit() | |
# arrange the output file's name and path | |
img_base = args.image[:args.image.rfind(".")] | |
#extension = args.image[args.image.rfind("."):] | |
extension = ".png" | |
path = args.image[:args.image.rfind("/")] | |
img_out = img_base+"[cropped]"+extension if args.output == None else args.output | |
# get the current image' size | |
data = subprocess.check_output(["identify", args.image]).decode("utf-8").strip().replace(args.image, "") | |
size = [int(n) for n in data.replace(args.image, "").split()[1].split("x")] | |
# calculate the command to resize | |
w = str(size[0] - args.left - args.right) | |
h = str(size[1] - args.top - args.bottom) | |
x = str(args.left) | |
y = str(args.top) | |
# execute the command | |
cmd = ["convert", args.image, "-crop", f"{w}x{h}+{x}+{y}", "+repage", img_out] | |
subprocess.Popen(cmd) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Description: Crop all images in a directory | |
if [ "$#" -ne 6 ]; then | |
echo "Usage: ./crop_dir.sh {directory} {output_directory} {left} {right} {top} {bottom}" | |
exit 1 | |
fi | |
directory="$1" | |
output_directory="$2" | |
left="$3" | |
right="$4" | |
top="$5" | |
bottom="$6" | |
mkdir -p "$output_directory" | |
find "$directory" -name "*.png" -maxdepth 1 -type f | while IFS= read -r f | |
do | |
echo "$f" | |
python3 crop.py "$f" "$left" "$right" "$top" "$bottom" -o "$output_directory/$(basename "${f}")" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment