Last active
September 17, 2017 09:35
-
-
Save sbrl/4661f0584082f1c97b633d6900dc1d6c to your computer and use it in GitHub Desktop.
Bash script to resize an image using imagemagick. Uses yad to provide a simple GUI. #bash #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 bash | |
| width=$(identify -ping -format "%w" "$1") | |
| height=$(identify -ping -format "%h" "$1") | |
| raw_options=$(yad --center --title "Resize image" --form --float-precision 0 --field "Filename":RO --field "Width":NUM --field "Height":NUM --field "Preserve aspect ratio":CHK "$1" "$width" "$height" true) | |
| # 0: Filename | |
| # 1: Width | |
| # 2: Height | |
| # 3: Preserve aspect ratio? | |
| options=() | |
| while read -d "|" option; do | |
| options+=("$option") | |
| done < <(echo "$raw_options") | |
| newfilename=${options[0]%.*}.resize.${options[0]##*.} | |
| echo ${options[0]} --\> ${newfilename} | |
| if [[ "${options[3]}" == "TRUE" ]]; then | |
| echo Preserving aspect ratio. | |
| convert "${options[0]}" -colorspace RGB -resize ${options[1]}x${options[2]} -colorspace sRGB "${newfilename}" | |
| else | |
| echo Not preserving aspect ratio. | |
| convert "${options[0]}" -colorspace RGB -resize ${options[1]}x${options[2]}! -colorspace sRGB "${newfilename}" | |
| fi | |
| new_width=$(identify -ping -format "%w" "${newfilename}") | |
| new_height=$(identify -ping -format "%h" "${newfilename}") | |
| echo ${width}x${height} --\> ${new_width}x${new_height} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment