Created
October 24, 2024 16:06
-
-
Save isurfer21/25eb54f7d165d5232a9f32595dd0a31c to your computer and use it in GitHub Desktop.
ImageMagick based command-line utility shell script to convert `png` to `jpg` with white background if having transparent background by default
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to print help message | |
function print_help() { | |
app_name=${0##*/} | |
echo "Syntax: $app_name <input_image> <output_image>" | |
echo " -h: Display this help message" | |
echo "Usage: " | |
echo " $app_name input.png output.jpg" | |
echo " $app_name '*.png' output.jpg" | |
} | |
# Check if help flag is provided | |
if [[ "$1" == "-h" ]]; then | |
print_help | |
exit 0 | |
fi | |
# Check if input file is provided | |
if [[ $# -lt 1 ]]; then | |
echo "Error: Input file is required." | |
print_help | |
exit 1 | |
fi | |
# Assign input and output files | |
input_file="$1" | |
# If output file is not provided, use input file name with output extension | |
if [[ $# -eq 1 ]]; then | |
output_file="${input_file%.png}.jpg" | |
else | |
output_file="$2" | |
fi | |
# Convert the image | |
convert "$input_file" -background white -alpha remove -alpha off "$output_file" | |
if [[ $? -ne 0 ]]; then | |
echo "Error: Image conversion failed." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment