<commit_before># -----------------------------------------------------------------------------
# ~zozo/.config/bash/functions/images.bash
# -----------------------------------------------------------------------------

dim()
{   # return image dimensions
    if [[ $# -eq 0 ]]; then
        printf "Usage: %s image ...\n" $FUNCNAME 1>&2
        return 1
    fi

    declare img width height
    for img in "$@"; do
        if sips --getProperty format "$img" 2>&1 | grep -q '^Error'; then
            printf "%s: Not an image file\n" "$img"
        else
            printf "%s: " "${img##*/}"

            sips --getProperty pixelWidth --getProperty pixelHeight "$img" |
            sed -nzE 's/^.*pixelWidth: ([[:digit:]]+)\n.*pixelHeight: ([[:digit:]]+)/\1 × \2/p'
        fi
    done
}
<commit_msg>new functions: getProperty() and maxWidth()<commit_after># -----------------------------------------------------------------------------
# ~zozo/.config/bash/functions/images.bash
# -----------------------------------------------------------------------------

getProperty()
{   # wrapper for sips to strip all the non-property output
    # Usage: getProperty PROPERTY FILE

    declare property="$1" file="$2"

    sips --getProperty $property "$file" |
        sed -nE "s%^[[:space:]]+$property: (.+)\$%\\1%p"
}

dim()
{   # return image dimensions
    if [[ $# -eq 0 ]]; then
        printf "Usage: %s image ...\n" $FUNCNAME 1>&2
        return 1
    fi

    declare img width height
    for img in "$@"; do
        if getProperty format "$img" 2>&1 | grep -q '^Error'; then
            scold "$img: Not an image file"
        else
            printf "%s: " "${img##*/}"

            sips --getProperty pixelWidth --getProperty pixelHeight "$img" |
            sed -nzE 's/^.*pixelWidth: ([[:digit:]]+)\n.*pixelHeight: ([[:digit:]]+)/\1 × \2/p'
        fi
    done
}

maxWidth()
{   # resize image $1 to $2 pixels wide
    
    declare imageFile="$1" newWidth="$2" newHeight
    declare originalWidth originalHeight aspectRatio

    # get current width and height
    originalWidth=$(getProperty pixelWidth "$imageFile")
    originalHeight=$(getProperty pixelHeight "$imageFile")

    # calculate aspect ratio
    aspectRatio=$(calc "scale=3;$originalWidth/$originalHeight")

    # calculate new height
    newHeight=$(calc "scale=0;$newWidth/$aspectRatio")

    # resize image
    /usr/bin/sips -z $newHeight $newWidth "$imageFile" &>/dev/null
}

{'fraction_non_alphanumeric': 0.2692778457772338, 'fraction_numerical': 0.010199918400652794, 'mean_word_length': 1.645091693635383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2729419', 'n_tokens_mistral': 710, 'n_tokens_neox': 676, 'n_words': 200}