Last active
July 15, 2024 19:30
-
-
Save imkh/1e349de95879d22445550f3ac222fc0f to your computer and use it in GitHub Desktop.
Revisions
-
imkh revised this gist
Jul 29, 2021 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -6,11 +6,11 @@ Requires [imagemagick](https://imagemagick.org/) (`brew install imagemagick` on ```console $ cd [directory] $ ./split_landscape.sh ``` or ```console $ ./split_landscape.sh [directory] ``` -
imkh created this gist
Jul 29, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ # split_landscape.sh Find all landscape images in a directory (sub directories included) and split them in half from right to left. For the opposite direction, remove the magick `-reverse` option on line 32. Requires [imagemagick](https://imagemagick.org/) (`brew install imagemagick` on macOS). ```console $ cd [directory] $ ./split.sh ``` or ```console $ ./split.sh [directory] ``` 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ #!/usr/bin/env sh if [[ -z "$1" ]]; then DIRECTORY="." else DIRECTORY="$1" fi echo "🔄 Looking for image files in \"$DIRECTORY\"" # Loop recursively through image files in a directory ### Alternative method to find image files (requires GNU grep: brew install grep) ### find "$DIRECTORY" -name '*' -exec file {} \; | ggrep -o -P '^.+: \w+ image' find "$DIRECTORY" -type f -print0 | xargs -0 file --mime-type | grep -F 'image/' | cut -d ':' -f 1 | while read FILE_PATH; do r=$(magick identify -format '%[fx:(w>h)]' "$FILE_PATH") # Check whether the file is portrait or landscape if [[ r -eq 1 ]] # 0 -> landscape and 1 -> portrait then # FILE_PATH = ``../directory/image.jpg` FILE_NAME="${FILE_PATH%.*}" # FILE_NAME = `../directory/image` FILE_EXTENSION="${FILE_PATH##*.}" # FILE_EXTENSION = `jpg` OUTPUT=""$FILE_NAME"_%d.$FILE_EXTENSION" # OUTOUT = `../directory/image_%d.jpg` magick identify -format "%[input] %[magick] %[width]x%[height] %[bit-depth]-bit %[colorspace]\n" "$FILE_PATH" ## Debugging # echo "FILE_PATH = $FILE_PATH" # echo "FILE_NAME = $FILE_NAME" # echo "FILE_EXTENSION = $FILE_EXTENSION" # echo "OUTPUT = $OUTPUT" # echo "----------" magick "$FILE_PATH" -crop 2x1@ -reverse +repage "$OUTPUT" # Split image file in half with output file names reversed for right-to-left rm "$FILE_PATH" # Delete landscape file fi done echo "✅ Done!"