Skip to content

Instantly share code, notes, and snippets.

@knbknb
Created December 15, 2024 18:23
Show Gist options
  • Save knbknb/bf4c5a03e6e51559a3d3595b9b00c129 to your computer and use it in GitHub Desktop.
Save knbknb/bf4c5a03e6e51559a3d3595b9b00c129 to your computer and use it in GitHub Desktop.
recraft-vectorize
#!/usr/bin/env bash
RECRAFT_API_KEY=...
now=$(date +"%Y%m%d_%H%M%S")
# Check if an input argument is provided and validate the file extension
if [ -z "$1" ]; then
echo "Error: No input file provided. Please specify a Webp, PNG or JPG file as the first argument."
exit 1
fi
if [[ ! "$1" =~ \.(webp|png|jpg|jpeg)$ ]]; then
echo "Error: The input file must be a WEBP, PNG or JPG file."
exit 1
fi
infile_path=$1
# remove extension from the file
infile_path_no_ext=${infile_path%.*}
infile_outfile="${infile_path_no_ext}.svg"
temp_response_file=response_$now.json
recraft_url="https://external.api.recraft.ai/v1/images/vectorize"
# Submit the request to the API and store the response in a temporary file
curl -s $recraft_url \
-H "Authorization: Bearer $RECRAFT_API_KEY" \
-H "Content-Type: multipart/form-data" \
-F "file=@$infile_path" > $temp_response_file
# Extract the URL from the response
image_url=$(jq -r '.image.url' < $temp_response_file)
if [ -n "$image_url" ]; then
echo "Downloading image from $image_url..."
curl -s "$image_url" -o "$infile_outfile"
echo "Image downloaded and saved as $infile_outfile"
else
echo "Error: Could not extract image URL from API response"
cat $temp_response_file
fi
# Clean up temporary response file
rm -f $temp_response_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment