Skip to content

Instantly share code, notes, and snippets.

@Tedfulk
Last active August 2, 2024 15:05
Show Gist options
  • Save Tedfulk/77b884453aec4c7f7bce95a5bb5818ae to your computer and use it in GitHub Desktop.
Save Tedfulk/77b884453aec4c7f7bce95a5bb5818ae to your computer and use it in GitHub Desktop.
Converts the images, logs which ones have been converted, also option to delete the webp files that were copied.
function convert_and_move_dalle_images
# Parse the arguments
set delete_flag 0
for arg in $argv
switch $arg
case '-d' '--delete'
set delete_flag 1
end
end
# Store the current directory
set original_dir (pwd)
# Define the directories
set downloads_dir ~/Downloads
set src_dir ~/Pictures/ai_generated/webp/
set dst_dir ~/Pictures/ai_generated/jpg/
set log_file $src_dir/converted_files.log
# Ensure the source and destination directories exist
mkdir -p $src_dir
mkdir -p $dst_dir
# Ensure the log file exists
touch $log_file
# Move .webp files from Downloads to the source directory
for file in $downloads_dir/*.webp
if test -f $file
mv $file $src_dir
end
end
# Change to the source directory
cd $src_dir
# Loop through all .webp files in the source directory
for file in *.webp
# Check if the file has already been processed
if not grep -q $file $log_file
# Extract the base filename without extension
set base_filename (basename $file .webp)
# Remove "DALL·E" from the filename if present
set new_filename (string replace -r "DALL·E" "" $base_filename)
# Remove date patterns from the filename
set new_filename (string replace -r "(January|February|March|April|May|June|July|August|September|October|November|December|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d{1,2}" "" $new_filename)
set new_filename (string replace -r "\d{1,2}/\d{1,2}" "" $new_filename)
# Trim leading/trailing spaces and hyphens
set new_filename (string trim -c " -" $new_filename)
# Replace spaces with dashes
set new_filename (string replace -a " " "-" $new_filename)
# Ensure unique filename
set counter 1
set final_filename $new_filename
while test -f $dst_dir$final_filename.jpg
set final_filename $new_filename"_"$counter
set counter (math $counter + 1)
end
# Convert the .webp file to .jpg using ffmpeg
ffmpeg -i $file -update 1 -frames:v 1 $dst_dir$final_filename.jpg
# Log the processed file
echo $file >> $log_file
# Delete the original .webp file if the delete flag is set
if test $delete_flag -eq 1
rm $file
end
end
end
# Change back to the original directory
cd $original_dir
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment