Created
January 30, 2026 21:49
-
-
Save ycmjason/78398958a321615dca1a26b27be9a8ec to your computer and use it in GitHub Desktop.
image utils
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
| # 1. CROP SQUARE | |
| # Usage: img_sq input.jpg | |
| # Description: Crops the image to the largest possible center square. | |
| # Output: input_sq.png | |
| img_sq() { | |
| local input="$1" | |
| if [ -z "$input" ]; then echo "Usage: img_sq <input_file>"; return 1; fi | |
| local filename="${input%.*}" | |
| ffmpeg -i "$input" -vf "crop=w=min(iw\,ih):h=min(iw\,ih)" "${filename}_sq.png" | |
| } | |
| # 2. CROP CIRCLE | |
| # Usage: img_circ input.jpg | |
| # Description: Crops center square, then applies a circular mask with anti-aliasing (smooth edges) and transparency. | |
| # Output: input_circle.png | |
| img_circ() { | |
| local input="$1" | |
| if [ -z "$input" ]; then echo "Usage: img_circ <input_file>"; return 1; fi | |
| local filename="${input%.*}" | |
| # Filter explanation: | |
| # 1. crop=... -> Cut the square | |
| # 2. format=rgba -> Add alpha channel | |
| # 3. geq=... -> Calculate distance from center for every pixel. | |
| # Uses 'clip' to create a soft anti-aliased edge. | |
| ffmpeg -i "$input" -vf "crop=w=min(iw\,ih):h=min(iw\,ih),format=rgba,geq=r='r(X,Y)':a='st(0,min(W,H)/2); st(1,hypot(X-W/2,Y-H/2)); clip((ld(0)-ld(1)+0.5),0,1)*255'" "${filename}_circle.png" | |
| } | |
| # 3. RESIZE (RATIO) | |
| # Usage: img_scale input.jpg 0.5 | |
| # Description: Resizes the image by a multiplier (e.g. 0.5 = 50% size). Keeps aspect ratio. | |
| # Output: input_x0.5.png | |
| img_scale() { | |
| local input="$1" | |
| local ratio="$2" | |
| if [ -z "$ratio" ]; then echo "Usage: img_scale <input_file> <ratio>"; return 1; fi | |
| local filename="${input%.*}" | |
| ffmpeg -i "$input" -vf "scale=iw*${ratio}:-1" "${filename}_x${ratio}.png" | |
| } | |
| # 4. SMART CONVERT | |
| # Usage: img_convert input.png webp OR img_convert input.png output.jpg | |
| # Description: Converts formats. If second arg is just an extension (e.g. 'webp'), it auto-names the file. | |
| # Output: input.webp OR output.jpg | |
| img_convert() { | |
| local input="$1" | |
| local dest="$2" | |
| if [ -z "$dest" ]; then | |
| echo "Usage: img_convert <input_file> <extension_or_filename>" | |
| return 1 | |
| fi | |
| # Smart Check: If $dest has no dot (e.g. "webp"), treat it as an extension | |
| # and auto-generate the filename based on the input name. | |
| if [[ "$dest" != *.* ]]; then | |
| local filename="${input%.*}" | |
| dest="${filename}.${dest}" | |
| fi | |
| # Optimization: If target is WebP, apply specific quality settings (q:v 80) | |
| if [[ "$dest" == *.webp ]]; then | |
| ffmpeg -i "$input" -c:v libwebp -q:v 80 "$dest" | |
| else | |
| ffmpeg -i "$input" "$dest" | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment