Skip to content

Instantly share code, notes, and snippets.

@iCross
Last active May 14, 2023 15:54
Show Gist options
  • Save iCross/10637e6a7b80cb16b06b0d831d315389 to your computer and use it in GitHub Desktop.
Save iCross/10637e6a7b80cb16b06b0d831d315389 to your computer and use it in GitHub Desktop.
One of my favorite zsh functions allows me to effortlessly upload .HEIC photos from my iPhone to macOS and automatically convert them to .jpg format. Furthermore, I can then utilize mozjpeg to compress the images, optimizing their size without compromising quality. MIT License.
function heic () {
for f in *.HEIC
do
[[ -e $f ]] || { echo "Error: File $f does not exist"; continue; }
output_file=${f:t:r}.jpg
/opt/homebrew/bin/convert -format jpg "$f" "$output_file" && \
/opt/homebrew/bin/trash "$f" || \
echo "Error: Conversion with convert failed for $f"
original_size=$(du -h "$output_file" | cut -f1)
tmp_file=$(mktemp /tmp/moz.XXXXXX.jpg)
/opt/homebrew/opt/mozjpeg/bin/cjpeg -quality 75 -progressive -outfile "$tmp_file" "$output_file" && \
mv "$tmp_file" "$output_file" || \
echo "Error: Compression with cjpeg failed for $output_file"
final_size=$(du -h "$output_file" | cut -f1)
echo "$output_file $original_size $final_size"
done
}
# IMG_5832.jpg 3.6M 1.4M
# IMG_5833.jpg 1.6M 576K
# IMG_5834.jpg 2.0M 720K
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment