Skip to content

Instantly share code, notes, and snippets.

@imkh
Last active July 15, 2024 19:30
Show Gist options
  • Save imkh/1e349de95879d22445550f3ac222fc0f to your computer and use it in GitHub Desktop.
Save imkh/1e349de95879d22445550f3ac222fc0f to your computer and use it in GitHub Desktop.

Revisions

  1. imkh revised this gist Jul 29, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original 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.sh
    $ ./split_landscape.sh
    ```

    or

    ```console
    $ ./split.sh [directory]
    $ ./split_landscape.sh [directory]
    ```
  2. imkh created this gist Jul 29, 2021.
    16 changes: 16 additions & 0 deletions README.md
    Original 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]
    ```
    37 changes: 37 additions & 0 deletions split_landscape.sh
    Original 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!"