Skip to content

Instantly share code, notes, and snippets.

@terracatta
Created July 3, 2023 08:43
Show Gist options
  • Save terracatta/1cf9124b9369db4060b5785abe0b025e to your computer and use it in GitHub Desktop.
Save terracatta/1cf9124b9369db4060b5785abe0b025e to your computer and use it in GitHub Desktop.
Convert png to jpg
#!/bin/bash
# Check if 'convert' is installed
if ! command -v convert &> /dev/null; then
echo "'convert' command not found. Attempting to install ImageMagick with Homebrew..."
# Check if 'brew' is installed
if ! command -v brew &> /dev/null; then
echo "'brew' command not found. Please install Homebrew and then run this script again."
exit 1
fi
# Install ImageMagick using Homebrew
brew install imagemagick
fi
# Define the directory where your blog assets are located
ASSETS_DIR="app/data/marketing/blog/articles/assets"
# Find all .png files in the directory and its subdirectories
find "$ASSETS_DIR" -name 'cover.png' | while read FILE; do
# Use ImageMagick's convert tool to convert the image to .jpg with quality set to 80
# The output filename is the same as the input filename, but with .jpg instead of .png
convert "$FILE" -quality 80 -interlace Plane "${FILE%.png}.jpg"
# Check if conversion was successful
if [ $? -eq 0 ]; then
# If the convert command was successful, remove the original .png file
rm "$FILE"
else
# If the convert command failed, print an error message
echo "Image conversion failed for $FILE"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment