Last active
March 17, 2024 18:21
-
-
Save gaborbata/a7529419eda1b22e8af7b66df7552b6d to your computer and use it in GitHub Desktop.
Convert images to HEIC
This file contains 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
#!/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