Skip to content

Instantly share code, notes, and snippets.

@gaborbata
Last active March 17, 2024 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaborbata/a7529419eda1b22e8af7b66df7552b6d to your computer and use it in GitHub Desktop.
Save gaborbata/a7529419eda1b22e8af7b66df7552b6d to your computer and use it in GitHub Desktop.
Convert images to HEIC
#!/usr/bin/env ruby
# Convert images recursively to heic
# Prereq.: sudo apt install libheif-examples libimage-exiftool-perl
require 'fileutils'
CONVERTERS = {
'heif-enc' => '-q 50 -o "%s" "%s"'
}
converter = CONVERTERS.find { |cmd, params| system("#{cmd}") }
Dir.glob('**/*.{jpg,png,PNG,JPG}').each do |image|
heic_img = image.sub(/\.(jpg|png|PNG|JPG)$/, '.heic')
if converter && !File.exist?(heic_img)
mtime = File.mtime(image)
puts "Converting #{image}..."
success = system("#{converter[0]} #{sprintf(converter[1], heic_img, image)}")
success_exif = system("exiftool -tagsFromFile #{image} #{heic_img}")
FileUtils.touch(heic_img, :mtime => mtime) if success
system("rm \"#{image}\"") if success
system("rm \"#{heic_img}_original\"") if success && success_exif
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment