Skip to content

Instantly share code, notes, and snippets.

@rietta
Created July 6, 2021 22:47
Show Gist options
  • Save rietta/14d1a04ad1af099624decdf5b462ef70 to your computer and use it in GitHub Desktop.
Save rietta/14d1a04ad1af099624decdf5b462ef70 to your computer and use it in GitHub Desktop.
Shell script to batch convert HEIC files to jpeg, leaving the original and its converted side by side. Requires Mac OS or Linux, the find command line tool, and ImageMagick
#!/usr/bin/env ruby
require 'shellwords'
files = `find . -iname '*.heic'`.split("\n")
files.each do |original_file|
output_file = original_file.gsub(/\.heic\z/i, ' Converted.jpg')
if File.exist?(output_file)
STDERR.puts "Skipping output #{output_file} exists."
else
cmd = "convert #{Shellwords.escape(original_file)} #{Shellwords.escape(output_file)}"
puts cmd
system cmd
end
en
@rietta
Copy link
Author

rietta commented Jul 6, 2021

  1. Create ~/bin/batch-convert-heic.rb
  2. Add the ~/bin folder to your path in your ZSH or bashrc
  3. chmod 755 ~/bin/batch-convert-heic.rb.
  4. Now from terminal, you can execute batch-convert-heic.rb to convert all HEIC files in any subfolder to JPEG.

@prgrmr-yn
Copy link

Thankyou, you are a legend :)

@Purp1eW0lf
Copy link

Needs a end on line 15, not en

@filipeandre
Copy link

convert not found

@RandelP
Copy link

RandelP commented Apr 17, 2024

@filipeandre This error message indicates that the convert command from ImageMagick is not installed on your Linux system, or it is not in the system's PATH.

@filipeandre
Copy link

filipeandre commented Apr 19, 2024

Thank you very much @RandelP .
I missed the requirement at gist description, I was expecting the comment at batch-convert-heic.rb body :)

@VGerris
Copy link

VGerris commented Sep 22, 2024

or without needing Ruby on Linux ( and perhaps OS X with brew ):

sudo apt-get install libheif-examples # ( install the library )
for i in *.heic; do heif-convert "$i" "${i%.*}.jpg"; done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment