fortunebot/teller.zsh

79 lines
2.5 KiB
Bash
Raw Normal View History

2025-01-01 05:43:09 +00:00
#!/bin/zsh
# Check if ImageMagick and fortune are installed
if ! command -v magick &>/dev/null || ! command -v fortune &>/dev/null; then
echo "Error: ImageMagick or fortune not installed. Please install both."
exit 1
fi
# Fetch the fortune message
if [[ "$1" == "samantha-mode" ]]; then
if ! ollama ls &>/dev/null; then
echo "Error: You can not use Samantha Mode because you are missing Ollama."
fi
FORTUNE="$(ollama run sparksammy/tinysam-l3.2:latest \"Please generate a pretend quote. Show only the quote.\")"
else
FORTUNE="$(fortune "$1")"
fi
# Check if the fortune command gave us any output
if [[ -z "$FORTUNE" ]]; then
echo "Error: fortune command returned empty output."
exit 1
fi
# Image dimensions and text settings
width=420 # Max width for the text
font="Arial" # Font to use (adjust to the font you have)
fontsize=24 # Font size
x_offset=10 # X position for the text
y_offset=30 # Starting Y position
# Format the fortune text to avoid issues with long lines or newlines
# Remove newlines and replace them with spaces (optional)
FORTUNE=$(echo "$FORTUNE" | tr '\n' ' ')
# Fetch a random image from Picsum
IMAGE_URL="https://picsum.photos/600"
TEMP_IMAGE="temp_image.jpg"
# Generate a random unique filename using sha256
TEMP_FORTUNE_IMAGE=$(echo "$RANDOM$RANDOM$RANDOM" | sha256sum | awk '{print $1}').jpg
# Download the image
wget -q "$IMAGE_URL" -O "$TEMP_IMAGE"
# Check if the image was downloaded successfully
if [[ ! -f "$TEMP_IMAGE" ]]; then
echo "Error: Failed to download image."
exit 1
fi
WRAPPED_FORTUNE=$(echo "$FORTUNE" | fold -s -w 42)
# Get the average color of the image (resize to 1x1 to get the average)
average_color=$(magick "$TEMP_IMAGE" -resize 1x1 -negate -format "%[pixel:u]" info:)
# Extract RGB values from the average color (format: rgb(R,G,B))
inverse_srgb=$(echo $average_color | sed 's/rgb(\([0-9]*\),\([0-9]*\),\([0-9]*\))/\1 \2 \3/')
# Use ImageMagick to add the fortune text to the image
# Use a default font if Arial is not available
# Add the fortune text directly on top of the image
magick "$TEMP_IMAGE" -size 600x -font "$font" -fill "$inverse_srgb" -pointsize "$fontsize" \
-gravity Center \
-annotate +$x_offset+$y_offset "$WRAPPED_FORTUNE" \
"$TEMP_FORTUNE_IMAGE"
# Check if the final image was created
if [[ ! -f "$TEMP_FORTUNE_IMAGE" ]]; then
echo "Error: Failed to create fortune image."
exit 1
fi
# Clean up the temporary image
rm "$TEMP_IMAGE"
# Provide the path to the generated image
echo "Your fortune image has been saved as: $TEMP_FORTUNE_IMAGE"