Skip to content

Instantly share code, notes, and snippets.

@alkcxy
Last active January 7, 2016 07:44
Show Gist options
  • Save alkcxy/e4e3a83ae1d8aec06d7d to your computer and use it in GitHub Desktop.
Save alkcxy/e4e3a83ae1d8aec06d7d to your computer and use it in GitHub Desktop.
Useful script to process (astronomical) image in batch
ext=JPG
extout=png
originalsizex=3888
originalsizey=2592
sizex=1920
sizey=1080
level=7.84%,78.43%,2.12
gamma=0.85,1.00,1.00
unsharp=5.0x1.0+1.0+3.0
elaborate=false
crop=false
timelapse=true
videoformat=hd1080
frame=24
vcodec=mpeg4
videoname=../video-timelapse.mp4
videobitrate=10000
audiobitrate=0
meltCustomParameter="-hide-audio"
vidstabParameter="shakiness=8 smoothing=20"

Useful script to process image in batch. You can pass 3 parameters:

  • $1 is the root of your project
  • $2 is the main setting file
  • $3 can override the main setting file for every subfolder of the "base" subfolder project

This project assumes that the source image files are under base/subdir, dark frames are under dark. Without arguments, $1 is current dir, $2 is settings.opts, $3 is *.opts.

This batch doesn't modify the source image files, but creates many copies of them so check your free space!

#!/bin/bash
if [ "$1" = "" ]; then
path=./
else
path=$1
fi
if [ "$2" = "" ]; then
settings=settings.opts
else
settings=$2
fi
if [ "$3" = "" ]; then
opt=*.opts
else
opt=$3
fi
cd "$path"
while read line
do
vars=`echo $line | awk '{match($1,"(.+)=(.+)",a)}END{print a[1]}'`
if [ "$vars" != "" ]; then
eval ${vars}=`echo $line | awk '{match($1,"(.+)=(.+)",a)}END{print a[2]}'`
fi
done < $settings
# fa la media dei dark frame, se presenti
if [ -d dark ]; then
convert dark/* -colorspace RGB -average master-dark.$extout
fi
for opts in `ls $opt`; do
while read line
do
vars=`echo $line | awk '{match($1,"(.+)=(.+)",a)}END{print a[1]}'`
if [ "$vars" != "" ]; then
eval ${vars}=`echo $line | awk '{match($1,"(.+)=(.+)",a)}END{print a[2]}'`
fi
done < $opts
# ci spostiamo nella dir che contiene le immagini grezze
# e le rinominiamo con numeri consecutivi
lowerext=$(echo $ext | awk '{print tolower($0)}')
if [ -d base ]; then
filesize=`ls base/$dir -m | wc -w`
mkdir -p light/$dir
echo "elaborazione di ${filesize} immagini nella directory ${dir}"
ls base/$dir/*.$ext -tr | nl -nrz -w5 | while read newname oldname
do
cp $oldname light/$dir/$newname.$lowerext
done
else
echo "La cartella non esiste"
exit 1
fi
if [ $elaborate = "true" ]; then
mkdir -p elaborate/$dir
for image in `ls light/$dir`
do
trans=$(echo $image | awk '{match($1, "(.*)\\.(.*)",a)}END{print a[1]}')
if [ ! -d dark ]; then
convert light/$dir/$image -colorspace RGB -level $level -gamma $gamma -unsharp $unsharp -quality 100 elaborate/$dir/$trans.$extout
else
convert master-dark.$extout light/$dir/$image -colorspace RGB -compose minus -composite -level $level -gamma $gamma -unsharp $unsharp -quality 100 elaborate/$dir/$trans.$extout
fi
done
fi
if [ $crop = "true" ]; then
mkdir -p cropped/$dir
if [ $timelapse = "true" ]; then
mkdir -p timelapse
fi
filesize=`ls elaborate/$dir -m | wc -w`
for image in `ls elaborate/$dir`
do
distx=$(($endx-$startx))
disty=$(($endy-$starty))
offsetx=$(($distx/$filesize))
offsety=$(($disty/$filesize))
coox=$(($startx-($sizex/2)))
cooy=$(($starty-($sizey/2)))
# calcolare gravity in base alla posizione immagine:
gravity=Center
# bordo basso per più della metà altezza nuova immagine: North
if [ "$(($originalsizey-$starty))" -lt "$(($sizey/2))" ] && [ "$startx" -gt "$(($sizex/2))" ] && [ "$(($originalsizex-$startx))" -gt "$(($sizex/2))" ]; then
gravity=North
fi
# bordo alto per più della metà altezza nuova immagine: South
if [ "$starty" -lt "$(($sizey/2))" ] && [ "$startx" -gt "$(($sizex/2))" ] && [ "$(($originalsizex-$startx))" -gt "$(($sizex/2))" ]; then
gravity=South
fi
# bordo sinistro per più della metà larghezza nuova immagine: East
if [ "$startx" -lt "$(($sizex/2))" ] && [ "$starty" -gt "$(($sizey/2))" ] && [ "$(($originalsizey-$starty))" -gt "$(($sizey/2))" ]; then
gravity=East
fi
# bordo destro per più della metà larghezza nuova immagine: West
if [ "$(($originalsizex-$startx))" -lt "$(($sizex/2))" ] && [ "$starty" -gt "$(($sizey/2))" ] && [ "$(($originalsizey-$starty))" -gt "$(($sizey/2))" ]; then
gravity=West
fi
# basso-sinistro: NorthEast
if [ "$(($originalsizey-$starty))" -lt "$(($sizey/2))" ] && [ "$startx" -lt "$(($sizex/2))" ] && [ "$(($originalsizex-$startx))" -gt "$(($sizex/2))" ]; then
gravity=NorthEast
fi
# alto-sinistro: SudEast
if [ "$starty" -lt "$(($sizey/2))" ] && [ "$startx" -lt "$(($sizex/2))" ] && [ "$(($originalsizex-$startx))" -gt "$(($sizex/2))" ]; then
gravity=SouthEast
fi
# alto-destro: SudWest
if [ "$starty" -lt "$(($sizey/2))" ] && [ "$startx" -gt "$(($sizex/2))" ] && [ "$(($originalsizex-$startx))" -lt "$(($sizex/2))" ]; then
gravity=SouthWest
fi
# basso-destro: NorthWest
if [ "$(($originalsizey-$starty))" -lt "$(($sizey/2))" ] && [ "$startx" -gt "$(($sizex/2))" ] && [ "$(($originalsizex-$startx))" -lt "$(($sizex/2))" ]; then
gravity=NorthWest
fi
trans=$(echo $image | awk '{match($1, "(.*)\\.(.*)",a)}END{print a[1]}')
convert elaborate/$dir/$image -colorspace RGB -crop ${sizex}x$sizey+$coox+$cooy -background black -compose Copy -gravity $gravity -extent ${sizex}x$sizey -quality 100 cropped/$dir/$trans.$extout
filesize=$(($filesize-1))
startx=$(($startx+$offsetx))
starty=$(($starty+$offsety))
echo "ancora ${filesize} immagini da elaborare"
if [ $timelapse = "true" ]; then
videosize=`ls video/ -m | wc -w`
videosize=$(($videosize+1))
videofilename=`seq -f "%05g" $videosize $videosize`
cp cropped/$dir/$trans.$extout timelapse/$videofilename.$extout
fi
done
fi
done
if [ $timelapse = "true" ]; then
cd timelapse
ls *.$extout -tr | nl -nrz -w5 | while read newname oldname
do
cp $oldname $newname.$extout
done
videocom=`command -v avconv || command -v ffmpeg`
echo "Creo il timelapse nel formato ${videoformat} con ${frame} al secondo"
$videocom -r ${frame} -i "%05d.${extout}" -r ${frame} -s ${videoformat} -vcodec $vcodec -qscale 1 -y $videoname
# if you want to "stabilize" the resulting video
# you can use melt and vid.stab
# instructions:
# http://bernaerts.dyndns.org/linux/74-ubuntu/329-ubuntu-trusty-rotate-stabilize-video-melt-vidstab
# http://public.hronopik.de/vid.stab/features.php?lang=en
# install with:
# sudo add-apt-repository ppa:sunab/kdenlive-release
# sudo apt-get update
# sudo apt-get install x264 melt
if [ `command -v melt` ]; then
melt $videoname -filter vidstab $vidstabParameter -consumer xml:input.mlt all=1
melt input.mlt -consumer avformat:stabilized-$videoname vcodec=libx264 b=$videobitrate acodec=aac ab=$audiobitrate tune=film preset=slow $meltCustomParameter
fi
fi
ext=jpg
dir=01
startx=965
starty=1875
endx=2720
endy=325
# settaggi specifici per set di immagini
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment