feat: add 4 utils

This commit is contained in:
Krzysztof kuhy Rudnicki 2025-11-14 15:44:34 +01:00
parent d32d305d3b
commit 57779a3e20
4 changed files with 496 additions and 0 deletions

View File

@ -0,0 +1,97 @@
#!/usr/bin/env bash
# Convert an image to a specified resolution
# Default resolution: 320x240
# Usage: image_to_resolution.sh <input_image> [resolution] [output_image]
set -euo pipefail
# Default resolution
DEFAULT_RESOLUTION="320x240"
# Function to display usage
usage() {
cat << EOF
Usage: $0 <input_image> [resolution] [output_image]
Arguments:
input_image Path to the input image file (required)
resolution Target resolution in WIDTHxHEIGHT format (default: ${DEFAULT_RESOLUTION})
output_image Path to the output image file (default: <input>_<resolution>.<ext>)
Examples:
$0 photo.jpg
$0 photo.jpg 640x480
$0 photo.jpg 1920x1080 output.jpg
$0 image.png 320x240 resized.png
Note: Requires ImageMagick (convert command)
EOF
exit 1
}
# Check if ImageMagick is installed
if ! command -v convert &> /dev/null; then
echo "Error: ImageMagick (convert) is not installed."
echo "Install it with:"
echo " Arch Linux: sudo pacman -S imagemagick"
echo " Ubuntu/Debian: sudo apt install imagemagick"
exit 1
fi
# Parse arguments
if [[ $# -lt 1 ]]; then
echo "Error: Missing required argument <input_image>"
usage
fi
INPUT_IMAGE="$1"
RESOLUTION="${2:-${DEFAULT_RESOLUTION}}"
OUTPUT_IMAGE="${3:-}"
# Validate input image exists
if [[ ! -f ${INPUT_IMAGE} ]]; then
echo "Error: Input image '${INPUT_IMAGE}' does not exist."
exit 1
fi
# Validate resolution format (WIDTHxHEIGHT)
if [[ ! ${RESOLUTION} =~ ^[0-9]+x[0-9]+$ ]]; then
echo "Error: Invalid resolution format '${RESOLUTION}'"
echo "Expected format: WIDTHxHEIGHT (e.g., 320x240, 1920x1080)"
exit 1
fi
# Generate output filename if not provided
if [[ -z ${OUTPUT_IMAGE} ]]; then
# Extract filename without extension and extension
BASENAME=$(basename "${INPUT_IMAGE}")
FILENAME="${BASENAME%.*}"
EXTENSION="${BASENAME##*.}"
# If no extension (single name file), default to jpg
if [[ ${FILENAME} == "${EXTENSION}" ]]; then
EXTENSION="jpg"
fi
# Create output filename with resolution suffix
DIRNAME=$(dirname "${INPUT_IMAGE}")
OUTPUT_IMAGE="${DIRNAME}/${FILENAME}_${RESOLUTION}.${EXTENSION}"
fi
# Perform the conversion
echo "Converting '${INPUT_IMAGE}' to ${RESOLUTION}..."
echo "Output will be saved to: ${OUTPUT_IMAGE}"
if convert "${INPUT_IMAGE}" -resize "${RESOLUTION}!" "${OUTPUT_IMAGE}"; then
echo "✓ Successfully converted image to ${RESOLUTION}"
echo "Output: ${OUTPUT_IMAGE}"
# Show file sizes
INPUT_SIZE=$(du -h "${INPUT_IMAGE}" | cut -f1)
OUTPUT_SIZE=$(du -h "${OUTPUT_IMAGE}" | cut -f1)
echo "Input size: ${INPUT_SIZE}"
echo "Output size: ${OUTPUT_SIZE}"
else
echo "✗ Error: Conversion failed"
exit 1
fi

117
scripts/utils/pdf_to_image.sh Executable file
View File

@ -0,0 +1,117 @@
#!/usr/bin/env bash
set -euo pipefail
# pdf_to_png.sh (magick-only backend, behaves like pdf_to_image)
#
# Convert one or more PDF files to image files using ImageMagick v7 `magick`.
# Default output format is jpg, but can be changed with -f.
OUTPUT_DIR=""
OUTPUT_FORMAT="jpg"
PDF_FILES=()
log() {
printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"
}
usage() {
cat << EOF
Usage:
$(basename "$0") [OPTIONS] PDF_FILE [PDF_FILE...]
Convert one or more PDF files to images using ImageMagick 'magick'.
Options:
-o DIR Output directory (default: current directory)
-f FORMAT Output image format (default: jpg)
-h Show this help
Examples:
$(basename "$0") file.pdf
$(basename "$0") -f png file1.pdf file2.pdf
$(basename "$0") -o out -f webp file.pdf
EOF
}
ensure_magick() {
if ! command -v magick > /dev/null 2>&1; then
echo "Error: 'magick' (ImageMagick v7) is not installed or not in PATH." >&2
exit 1
fi
}
parse_args() {
local opt
OUTPUT_DIR=""
OUTPUT_FORMAT="jpg"
PDF_FILES=()
while getopts ":o:f:h" opt; do
case "$opt" in
o)
OUTPUT_DIR="$OPTARG"
;;
f)
OUTPUT_FORMAT="$OPTARG"
;;
h)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [[ $# -lt 1 ]]; then
echo "Error: at least one PDF file must be specified." >&2
usage
exit 1
fi
PDF_FILES=("$@")
if [[ -z ${OUTPUT_DIR:-} ]]; then
OUTPUT_DIR="${PWD}"
fi
if [[ ! -d $OUTPUT_DIR ]]; then
mkdir -p "$OUTPUT_DIR"
fi
}
convert_pdf() {
local pdf_file="$1"
local base name out_pattern
name="$(basename "$pdf_file")"
base="${name%.*}"
out_pattern="${OUTPUT_DIR%/}/${base}_page-"
log "Converting '$pdf_file' to $OUTPUT_FORMAT using magick -> ${out_pattern}*.${OUTPUT_FORMAT}"
magick -density 300 "$pdf_file" -quality 90 "${out_pattern}%d.${OUTPUT_FORMAT}"
}
main() {
ensure_magick
parse_args "$@"
local pdf
for pdf in "${PDF_FILES[@]}"; do
if [[ ! -f $pdf ]]; then
echo "Warning: '$pdf' is not a regular file, skipping." >&2
continue
fi
convert_pdf "$pdf"
done
log "Done converting PDFs to ${OUTPUT_FORMAT}. Output directory: $OUTPUT_DIR"
}
main "$@"

117
scripts/utils/pdf_to_png.sh Executable file
View File

@ -0,0 +1,117 @@
#!/usr/bin/env bash
set -euo pipefail
# pdf_to_png.sh (magick-only backend, behaves like pdf_to_image)
#
# Convert one or more PDF files to image files using ImageMagick v7 `magick`.
# Default output format is jpg, but can be changed with -f.
OUTPUT_DIR=""
OUTPUT_FORMAT="jpg"
PDF_FILES=()
log() {
printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"
}
usage() {
cat << EOF
Usage:
$(basename "$0") [OPTIONS] PDF_FILE [PDF_FILE...]
Convert one or more PDF files to images using ImageMagick 'magick'.
Options:
-o DIR Output directory (default: current directory)
-f FORMAT Output image format (default: jpg)
-h Show this help
Examples:
$(basename "$0") file.pdf
$(basename "$0") -f png file1.pdf file2.pdf
$(basename "$0") -o out -f webp file.pdf
EOF
}
ensure_magick() {
if ! command -v magick > /dev/null 2>&1; then
echo "Error: 'magick' (ImageMagick v7) is not installed or not in PATH." >&2
exit 1
fi
}
parse_args() {
local opt
OUTPUT_DIR=""
OUTPUT_FORMAT="jpg"
PDF_FILES=()
while getopts ":o:f:h" opt; do
case "$opt" in
o)
OUTPUT_DIR="$OPTARG"
;;
f)
OUTPUT_FORMAT="$OPTARG"
;;
h)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [[ $# -lt 1 ]]; then
echo "Error: at least one PDF file must be specified." >&2
usage
exit 1
fi
PDF_FILES=("$@")
if [[ -z ${OUTPUT_DIR:-} ]]; then
OUTPUT_DIR="${PWD}"
fi
if [[ ! -d $OUTPUT_DIR ]]; then
mkdir -p "$OUTPUT_DIR"
fi
}
convert_pdf() {
local pdf_file="$1"
local base name out_pattern
name="$(basename "$pdf_file")"
base="${name%.*}"
out_pattern="${OUTPUT_DIR%/}/${base}_page-"
log "Converting '$pdf_file' to $OUTPUT_FORMAT using magick -> ${out_pattern}*.${OUTPUT_FORMAT}"
magick -density 300 "$pdf_file" -quality 90 "${out_pattern}%d.${OUTPUT_FORMAT}"
}
main() {
ensure_magick
parse_args "$@"
local pdf
for pdf in "${PDF_FILES[@]}"; do
if [[ ! -f $pdf ]]; then
echo "Warning: '$pdf' is not a regular file, skipping." >&2
continue
fi
convert_pdf "$pdf"
done
log "Done converting PDFs to ${OUTPUT_FORMAT}. Output directory: $OUTPUT_DIR"
}
main "$@"

165
scripts/utils/txt_to_image.sh Executable file
View File

@ -0,0 +1,165 @@
#!/usr/bin/env bash
# Convert a text file to image(s) with specified resolution
# Default resolution: 320x240
# Automatically splits text into multiple images if it doesn't fit
# Usage: txt_to_image.sh <input_text_file> [resolution] [output_prefix]
set -euo pipefail
# Default resolution
DEFAULT_RESOLUTION="320x240"
# Function to display usage
usage() {
cat << EOF
Usage: $0 <input_text_file> [resolution] [output_prefix]
Arguments:
input_text_file Path to the input text file (required)
resolution Target resolution in WIDTHxHEIGHT format (default: ${DEFAULT_RESOLUTION})
output_prefix Prefix for output image files (default: <input_filename>)
Examples:
$0 notes.txt
$0 notes.txt 640x480
$0 notes.txt 320x240 output
Note: Requires ImageMagick (magick or convert command)
EOF
exit 1
}
# Check if ImageMagick is installed and determine which command to use
if command -v magick &> /dev/null; then
MAGICK_CMD="magick"
elif command -v convert &> /dev/null; then
MAGICK_CMD="convert"
else
echo "Error: ImageMagick is not installed."
echo "Install it with:"
echo " Arch Linux: sudo pacman -S imagemagick"
echo " Ubuntu/Debian: sudo apt install imagemagick"
exit 1
fi
# Parse arguments
if [[ $# -lt 1 ]]; then
echo "Error: Missing required argument <input_text_file>"
usage
fi
INPUT_FILE="$1"
RESOLUTION="${2:-${DEFAULT_RESOLUTION}}"
OUTPUT_PREFIX="${3:-}"
# Validate input file exists
if [[ ! -f ${INPUT_FILE} ]]; then
echo "Error: Input file '${INPUT_FILE}' does not exist."
exit 1
fi
# Validate resolution format (WIDTHxHEIGHT)
if [[ ! ${RESOLUTION} =~ ^[0-9]+x[0-9]+$ ]]; then
echo "Error: Invalid resolution format '${RESOLUTION}'"
echo "Expected format: WIDTHxHEIGHT (e.g., 320x240, 1920x1080)"
exit 1
fi
# Extract width and height
WIDTH=$(echo "${RESOLUTION}" | cut -d'x' -f1)
HEIGHT=$(echo "${RESOLUTION}" | cut -d'x' -f2)
# Calculate font size based on resolution
FONT_SIZE=$((WIDTH / 30))
if [[ ${FONT_SIZE} -lt 8 ]]; then
FONT_SIZE=8
fi
# Generate output prefix if not provided
if [[ -z ${OUTPUT_PREFIX} ]]; then
BASENAME=$(basename "${INPUT_FILE}")
FILENAME="${BASENAME%.*}"
DIRNAME=$(dirname "${INPUT_FILE}")
OUTPUT_PREFIX="${DIRNAME}/${FILENAME}"
fi
# Calculate lines per image based on resolution and font size
# Rough estimate: height / (font_size * 1.5) for line spacing
LINES_PER_IMAGE=$((HEIGHT / (FONT_SIZE * 3 / 2)))
if [[ ${LINES_PER_IMAGE} -lt 5 ]]; then
LINES_PER_IMAGE=5
fi
echo "Converting text file to image(s)..."
echo "Resolution: ${RESOLUTION}"
echo "Font size: ${FONT_SIZE}"
echo "Estimated lines per image: ${LINES_PER_IMAGE}"
# Read the file and count total lines
mapfile -t LINES < "${INPUT_FILE}"
TOTAL_LINES=${#LINES[@]}
echo "Total lines in file: ${TOTAL_LINES}"
# Calculate number of images needed
NUM_IMAGES=$(((TOTAL_LINES + LINES_PER_IMAGE - 1) / LINES_PER_IMAGE))
echo "Creating ${NUM_IMAGES} image(s)..."
# Create temporary directory for chunks
TEMP_DIR=$(mktemp -d)
trap 'rm -rf ${TEMP_DIR}' EXIT
# Split text into chunks and create images
IMAGE_COUNT=0
for ((i = 0; i < TOTAL_LINES; i += LINES_PER_IMAGE)); do
IMAGE_COUNT=$((IMAGE_COUNT + 1))
# Calculate end line for this chunk
END_LINE=$((i + LINES_PER_IMAGE))
if [[ ${END_LINE} -gt ${TOTAL_LINES} ]]; then
END_LINE=${TOTAL_LINES}
fi
# Create chunk file
CHUNK_FILE="${TEMP_DIR}/chunk_${IMAGE_COUNT}.txt"
for ((j = i; j < END_LINE; j++)); do
echo "${LINES[$j]}" >> "${CHUNK_FILE}"
done
# Determine output filename
if [[ ${NUM_IMAGES} -eq 1 ]]; then
OUTPUT_FILE="${OUTPUT_PREFIX}.png"
else
OUTPUT_FILE="${OUTPUT_PREFIX}_$(printf "%03d" ${IMAGE_COUNT}).png"
fi
echo " Creating image ${IMAGE_COUNT}/${NUM_IMAGES}: ${OUTPUT_FILE}"
# Create image from text
# Using label: instead of caption: for better control
if ${MAGICK_CMD} -size "${WIDTH}x${HEIGHT}" \
-background white \
-fill black \
-font "DejaVu-Sans-Mono" \
-pointsize "${FONT_SIZE}" \
-gravity northwest \
label:@"${CHUNK_FILE}" \
-extent "${WIDTH}x${HEIGHT}" \
"${OUTPUT_FILE}"; then
OUTPUT_SIZE=$(du -h "${OUTPUT_FILE}" | cut -f1)
echo " ✓ Created: ${OUTPUT_FILE} (${OUTPUT_SIZE})"
else
echo " ✗ Failed to create: ${OUTPUT_FILE}"
exit 1
fi
done
echo ""
echo "✓ Successfully created ${IMAGE_COUNT} image(s)"
echo "Output files:"
if [[ ${NUM_IMAGES} -eq 1 ]]; then
echo " ${OUTPUT_PREFIX}.png"
else
echo " ${OUTPUT_PREFIX}_001.png to ${OUTPUT_PREFIX}_$(printf "%03d" ${IMAGE_COUNT}).png"
fi