Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert, resize and paint images from JPG to PNG using imagemagick
#!/bin/bash
set -e
if [ "$1" == "" ] || [ ! -d "$1" ]; then
echo "Invalid Path $1"
exit 1
fi
LC_NUMERIC="en_US.UTF-8"
function timerelative {
local T=$(printf "%.0f" $1)
local D=$(( $T / 60 / 60 / 24 ))
local H=$(( $T / 60 / 60 % 24 ))
local M=$(( $T / 60 % 60 ))
local S=$(( $T % 60 ))
printf '%02dd %02dh %02dm %02ds' "$D" "$H" "$M" "$S"
}
function progress {
COUNT=$(( $COUNT + 1 ))
local NOW=$(date +%s.%3N)
local TIME=$(echo "$NOW - $START" | bc -l)
local AVG=$(echo "$TIME / $COUNT" | bc -l)
local MAX=$(echo "$AVG * $TOTAL" | bc -l)
local FINISH=$(echo "$MAX - $TIME" | bc -l)
local CURRENT=$(echo "$NOW - $MICRO" | bc -l)
AVG=$(printf "%.2f" $AVG)
CURRENT=$(printf "%.2f" $CURRENT)
echo ""
echo -n "[$(date +"%F %T")] "
echo -n "[$(printf "%0${ZEROS}d" $COUNT) / $TOTAL] "
echo -n "[TOTAL $(timerelative $MAX)] "
echo -n "[ELAPSED $(timerelative $TIME)] "
echo -n "[FINISH $(timerelative $FINISH)] "
echo -n "[AVG ${AVG}s] "
echo -n "[CURRENT ${CURRENT}s] "
echo -n "$1"
MICRO=$NOW
}
cd $1
TOTAL=0
echo -e "\n[$(date +"%F %T")] Calculate Total over $(ls *.jpeg | wc -l) files"
for jpg in *.jpeg; do
png=$(echo $jpg | sed -e 's/jpeg/png/')
step1=$(echo $jpg | sed -e 's/\.jpeg/-large.png/')
step2=$(echo $jpg | sed -e 's/\.jpeg/-large-blur-1-paint-2.png/')
if [ ! -f $png ]; then
TOTAL=$(( $TOTAL + 1 ))
fi
if [ ! -f $step1 ]; then
TOTAL=$(( $TOTAL + 1 ))
fi
if [ ! -f $step2 ]; then
TOTAL=$(( $TOTAL + 1 ))
fi
done
echo -e "\n[$(date +"%F %T")] Start Processing $TOTAL files"
COUNT=0
MICRO=0
START=$(date +%s.%3N)
ZEROS=${#TOTAL}
for jpg in *.jpeg; do
if [ $COUNT == 0 ]; then
MICRO=$(date +%s.%3N)
fi
png=$(echo $jpg | sed -e 's/jpeg/png/')
step1=$(echo $jpg | sed -e 's/\.jpeg/-large.png/')
step2=$(echo $jpg | sed -e 's/\.jpeg/-large-blur-1-paint-2.png/')
if [ ! -f $png ]; then
convert $jpg $png
progress "[CONVERT] [FROM] $png [TO] $step1"
fi
if [ ! -f $step1 ]; then
convert $png -filter point -resize 200% -unsharp 0x3+0.5+0 -resize 200% -unsharp 0x3+0.5+0 -strip $step1
progress "[RESIZE] [FROM] $png [TO] $step1"
fi
if [ ! -f $step2 ]; then
convert $step1 -blur 0x1 -paint 2 $step2
progress "[PAINT] [FROM] $step1 [TO] $step2"
fi
done
echo -e "\n[$(date +"%F %T")] Finish"
eog *large*.png
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment