Skip to content

Instantly share code, notes, and snippets.

@4rmx
Created July 22, 2026 13:15
Show Gist options
  • Select an option

  • Save 4rmx/c2b4f71aca69fd23aac471a1d20d4cda to your computer and use it in GitHub Desktop.

Select an option

Save 4rmx/c2b4f71aca69fd23aac471a1d20d4cda to your computer and use it in GitHub Desktop.
Screenshot with reduce quality script for mac
#!/bin/bash
# ==============================================================================
# Script Name: captureOptimize.sh
# Description: Automated macOS Screen Capture & Image Processing Utility
#
# Functionality:
# 1. Captures a specific region of the screen based on standard input arguments
# (X, Y, Width, Height) via native macOS `screencapture`.
# 2. Automatically falls back to an interactive selection mode (-i) if no
# coordinates are provided.
# 3. Reads plain text directly from the macOS system clipboard (pbpaste).
# 4. Processes the captured image using ImageMagick (`magick`):
# - Resizes image dimensions down to 85%.
# - Strips all internal metadata for optimal file size reduction.
# - Resamples image density to 72 DPI.
# - Annotates clipboard text at the top-right corner with a semi-transparent
# black background box and custom font styling.
# 5. Converts and exports the final output as a JPG file to a designated path.
# 6. Prints file details (Dimensions, File Size) to STDOUT and cleans up
# temporary processing files.
#
# Run with Shortcuts
# nohup ~/Scripts/captureOptimize.sh 268 362 1100 588 >/dev/null 2>&1 &
# ==============================================================================
# --- Configuration ---
MAGICK="/usr/local/bin/magick"
FONT_PATH="/Users/empty/Library/Fonts/GoogleSans-Regular.ttf"
TEMP_IMG="/tmp/capture_process.png"
OUTPUT_PATH="/Users/empty/Downloads/temp_resize.jpg"
# 1. Capture Area (Native macOS)
# If 4 arguments are passed ($1=X, $2=Y, $3=Width, $4=Height), use fixed rectangle (-R)
if [ "$#" -eq 4 ]; then
CAPTURE_RECT="$1,$2,$3,$4"
/usr/sbin/screencapture -R "$CAPTURE_RECT" -x "$TEMP_IMG"
else
# Fallback to interactive mode if no coordinates given
/usr/sbin/screencapture -i -x "$TEMP_IMG"
fi
# ตรวจสอบว่ามีไฟล์จากการ Capture หรือไม่
if [ -s "$TEMP_IMG" ]; then
# 2. ดึง Text จาก Clipboard
CLIP_TEXT=$(pbpaste)
# ถ้าไม่มี Text ใน Clipboard ให้เว้นว่างไว้
if [ -z "$CLIP_TEXT" ]; then
CLIP_TEXT=" "
fi
# 3. Image Processing: Resize, Strip, Resample, and Annotate
$MAGICK "$TEMP_IMG" \
-resize 85% \
-strip \
-resample 72 \
-font "$FONT_PATH" \
-pointsize 24 \
-fill white -undercolor '#00000080' \
-gravity Northeast -annotate +15+15 " $CLIP_TEXT" \
"$OUTPUT_PATH"
# 4. แสดง Log ข้อมูลไฟล์
FILE_SIZE=$(du -h "$OUTPUT_PATH" | cut -f1)
DIMENSIONS=$($MAGICK identify -format "%wx%h" "$OUTPUT_PATH")
echo "--- Process Complete ---"
echo "Text Added : $CLIP_TEXT"
echo "Dimensions : $DIMENSIONS"
echo "File Size : $FILE_SIZE"
echo "Saved to : $OUTPUT_PATH"
# 5. ลบไฟล์ Capture ชั่วคราว
rm "$TEMP_IMG"
else
echo "Capture cancelled."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment