Skip to content

Instantly share code, notes, and snippets.

@greg-randall
Created March 9, 2025 00:38
Show Gist options
  • Save greg-randall/5e1f76934f8f5ae5861ec4fcc6c22076 to your computer and use it in GitHub Desktop.
Save greg-randall/5e1f76934f8f5ae5861ec4fcc6c22076 to your computer and use it in GitHub Desktop.
This script processes a sequence of TIF images by cropping each image to a vertical strip from the center for use in slitscan image creation
#!/bin/bash
# This script processes a sequence of TIF images by cropping each image to a vertical strip from the center. Here's a breakdown:
# 1. It takes images from an input directory ("frames-try1") and saves the cropped versions to an output directory ("frames-03-trial").
# 2. The script extracts a vertical strip with a width of 105 pixels, centered in each image.
# 3. It uses ffprobe to determine the dimensions of the first image, calculates the center, and then uses ffmpeg to crop all images in the sequence.
# If you want to use this script, you might need to edit:
# - `INPUT_DIR` and `OUTPUT_DIR` paths to match your folder names
# - `STRIP_WIDTH` to change how wide the vertical strip should be
# - The file format if you're not using TIF files
# - The filename pattern in the ffmpeg command if your files aren't numbered as %06d.tif (6-digit numbers)
# The script is designed to maintain the vertical height of the images while only keeping a centered vertical strip.
# Input and output directories
INPUT_DIR="frames-try1"
OUTPUT_DIR="frames-03-trial"
# Strip width configuration (can be changed to any number of pixels)
STRIP_WIDTH=105
# Create output directory if it doesn't exist
mkdir -p "$OUTPUT_DIR"
# Check if the input directory exists
if [ ! -d "$INPUT_DIR" ]; then
echo "Error: Input directory '$INPUT_DIR' not found."
exit 1
fi
echo "Processing TIF sequence from '$INPUT_DIR' to '$OUTPUT_DIR'..."
echo "Strip width: $STRIP_WIDTH pixels"
# Change to input directory
cd "$INPUT_DIR"
# Find the first TIF file to get dimensions
first_file=$(find . -name "*.tif" | sort | head -n 1)
if [ -z "$first_file" ]; then
echo "Error: No TIF files found in '$INPUT_DIR'"
exit 1
fi
# Get image dimensions using ffprobe
width=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=p=0 "$first_file")
height=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 "$first_file")
if [ -z "$width" ] || [ -z "$height" ]; then
echo "Error: Could not determine dimensions for $first_file"
exit 1
fi
# Calculate center and adjust for strip width to keep it centered
center_x=$((width / 2))
strip_x=$((center_x - STRIP_WIDTH / 2))
echo "Image dimensions: ${width}x${height}"
echo "Center column: $center_x, Strip starting at: $strip_x, Width: $STRIP_WIDTH"
# Return to original directory
cd ..
# Process the entire sequence in one command
ffmpeg -i "$INPUT_DIR/%06d.tif" \
-vf "crop=$STRIP_WIDTH:$height:$strip_x:0" \
"$OUTPUT_DIR/%06d.tif"
echo "Completed processing sequence."s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment