Skip to content

Instantly share code, notes, and snippets.

@WillBishop
Created July 7, 2024 08:36
Show Gist options
  • Save WillBishop/b2ef8619006ef1a704a149efc42d3283 to your computer and use it in GitHub Desktop.
Save WillBishop/b2ef8619006ef1a704a149efc42d3283 to your computer and use it in GitHub Desktop.
Convert all images from png/jpeg to heif
# Convert .png and .jpeg files to .heif format, ensuring "xcassets" is in the path
# Depending on what folder your icons are in, you may wish to append ' -not -path "*/Icons/*"' to that find command so you don't convert all your icon assets.
find . -path "*xcassets*" \( -name "*.png" -o -name "*.jpeg" \) | while read -r file; do
# Get the filename without the extension
filename="${file%.*}"
# Run the magick command to convert the file to .heif
/opt/homebrew/bin/magick "$file" "$filename.heif"
rm "$file"
# Print a message indicating the conversion
echo "Converted $file to $filename.heif"
done
# Replace "png" and "jpeg" with "heif" in .json files, ensuring "xcassets" is in the path and excluding "*/Icons/*"
# Same warning as above!
find . -path "*xcassets*" -name "*.json" | while read -r file; do
# Check if the file contains "png" or "jpeg"
if grep -qE "png|jpeg" "$file"; then
# Use sed to find and replace "png" and "jpeg" with "heif" in the file
sed -i '' 's/png/heif/g; s/jpeg/heif/g' "$file"
# Print a message indicating the replacement
echo "Replaced 'png' and 'jpeg' with 'heif' in $file"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment