Skip to content

Instantly share code, notes, and snippets.

@prgrmr-yn
Created August 19, 2023 11:20
Show Gist options
  • Save prgrmr-yn/a13d0722a4d55ce7e806d0521a8c3a48 to your computer and use it in GitHub Desktop.
Save prgrmr-yn/a13d0722a4d55ce7e806d0521a8c3a48 to your computer and use it in GitHub Desktop.
Convert heic and webp files to jpg files
#!/usr/bin/env ruby
require 'shellwords'
def convert_to_jpg(original_file, output_file)
cmd = "convert #{Shellwords.escape(original_file)} #{Shellwords.escape(output_file)}"
success = system(cmd)
if success
File.delete(original_file) # Deletes the original file if the conversion was successful
puts "Deleted original file: #{original_file}"
else
puts "Conversion failed for file: #{original_file}"
end
end
def convert_heic_files
heic_files = `find . -iname '*.heic'`.split("\n")
heic_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
puts "Converting HEIC file: #{original_file}"
puts "New file: #{output_file}"
convert_to_jpg(original_file, output_file)
end
end
end
def convert_webp_files
webp_files = `find . -iname '*.webp'`.split("\n")
webp_files.each do |original_file|
output_file = original_file.gsub(/\.webp\z/i, ' Converted.jpg')
if File.exist?(output_file)
STDERR.puts "Skipping output #{output_file} exists."
else
puts "Converting WebP file: #{original_file}"
puts "New file: #{output_file}"
convert_to_jpg(original_file, output_file)
end
end
end
convert_heic_files
convert_webp_files
@prgrmr-yn
Copy link
Author

Most of the time when you airdrop image or download from internet, it goes to downloads with heic or webp
So what you wanna do is open your terminal

Step 1-cd /Users/${your computer name}/Downloads
Step 2- touch jpg-convertor.rb
Step 3- Open that file in text editor and paste the above code
Step 4- Now in your terminal type chmod 755 ./jpg-convertor.rb
Step 5- Simply run ./jpg-convertor.rb and see the magic

@prgrmr-yn
Copy link
Author

If you would like to make it a c executable and click the program instead
make a c file called jpg-convertor.c and in that file add the code

C CODE START:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
// Use the system function to execute the Ruby script
int result = system("ruby jpg-convertor.rb");

if (result == -1) {
perror("Error executing Ruby script");
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}
END C Code

Now compile the c code by typing in terminal:
gcc -o jpg-convertor jpg-convertor.c

Now you have the executable file that you can just double click to open.

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