Skip to content

Instantly share code, notes, and snippets.

@isurfer21
Created October 24, 2024 16:06
Show Gist options
  • Save isurfer21/25eb54f7d165d5232a9f32595dd0a31c to your computer and use it in GitHub Desktop.
Save isurfer21/25eb54f7d165d5232a9f32595dd0a31c to your computer and use it in GitHub Desktop.

Revisions

  1. isurfer21 created this gist Oct 24, 2024.
    41 changes: 41 additions & 0 deletions png2jpg.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/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