Created
September 10, 2025 19:45
-
-
Save jcheng5/ef4eeae5e357a62a923857d845345061 to your computer and use it in GitHub Desktop.
Extract depth from HEIC
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
| #!/bin/bash | |
| # extract_depth.sh - Extract depth map from HEIC files | |
| # Usage: ./extract_depth.sh input.heic | |
| # Check if argument was provided | |
| if [ $# -ne 1 ]; then | |
| echo "Usage: $0 <heic_file>" | |
| exit 1 | |
| fi | |
| INPUT_FILE="$1" | |
| # Check if input file exists | |
| if [ ! -f "$INPUT_FILE" ]; then | |
| echo "Error: File '$INPUT_FILE' not found" | |
| exit 1 | |
| fi | |
| # Check if input file is a HEIC file | |
| if [[ ! "$INPUT_FILE" =~ \.heic$ ]]; then | |
| echo "Error: File '$INPUT_FILE' is not a HEIC file" | |
| exit 1 | |
| fi | |
| # Check if heif-convert is installed | |
| if ! command -v heif-convert &> /dev/null; then | |
| echo "Error: heif-convert is not installed" | |
| echo "Install it using: brew install libheif" | |
| exit 1 | |
| fi | |
| # Get the base name without extension | |
| BASENAME=$(basename "$INPUT_FILE" .heic) | |
| OUTPUT_DIR=$(dirname "$INPUT_FILE") | |
| TEMP_OUTPUT="$OUTPUT_DIR/${BASENAME}_temp" | |
| echo "Extracting depth map from $INPUT_FILE..." | |
| # Extract depth map and other auxiliary images | |
| heif-convert --with-aux "$INPUT_FILE" "$TEMP_OUTPUT.jpg" | |
| # Check if extraction was successful | |
| if [ $? -ne 0 ]; then | |
| echo "Error: Failed to extract depth map" | |
| exit 1 | |
| fi | |
| # Also extract original image as JPG | |
| echo "Extracting original image as JPG..." | |
| heif-convert "$INPUT_FILE" "$OUTPUT_DIR/${BASENAME}.jpg" | |
| # Check if JPG extraction was successful | |
| if [ $? -ne 0 ]; then | |
| echo "Warning: Failed to extract original image as JPG" | |
| # Try using the temporary JPG if it exists | |
| if [ -f "${TEMP_OUTPUT}.jpg" ]; then | |
| echo "Using already extracted JPG..." | |
| cp "${TEMP_OUTPUT}.jpg" "$OUTPUT_DIR/${BASENAME}.jpg" | |
| if [ $? -eq 0 ]; then | |
| echo "Original image extracted to: ${BASENAME}.jpg" | |
| else | |
| echo "Warning: Failed to save original image" | |
| fi | |
| fi | |
| else | |
| echo "Original image extracted to: ${BASENAME}.jpg" | |
| fi | |
| # Rename the depth map file to keep the original basename | |
| if [ -f "${TEMP_OUTPUT}-depth.jpg" ]; then | |
| mv "${TEMP_OUTPUT}-depth.jpg" "$OUTPUT_DIR/${BASENAME}-depth.jpg" | |
| echo "Depth map extracted to: ${BASENAME}-depth.jpg" | |
| fi | |
| # Rename any other auxiliary images if they exist | |
| if ls "${TEMP_OUTPUT}-urn:"* &> /dev/null; then | |
| for aux_file in "${TEMP_OUTPUT}-urn:"*; do | |
| # Extract the auxiliary type from the filename | |
| AUX_TYPE=$(echo "$aux_file" | sed "s|${TEMP_OUTPUT}-||") | |
| mv "$aux_file" "$OUTPUT_DIR/${BASENAME}-${AUX_TYPE}" | |
| echo "Auxiliary image extracted to: ${BASENAME}-${AUX_TYPE}" | |
| done | |
| fi | |
| # Clean up the temporary main image file | |
| if [ -f "${TEMP_OUTPUT}.jpg" ]; then | |
| rm "${TEMP_OUTPUT}.jpg" | |
| fi | |
| echo "Extraction complete!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment