środa, 19 sierpnia 2015

Converting images tips

 Here is list of small bash programs that help you manage pictures.


for i in *.png ; do convert "$i" "${i%.*}.jpg" ; done

For some reason I tend to avoid loops in bash so here is a more unixy xargs approach, using bash for the name-mangling.

ls -1 *.png | xargs -n 1 bash -c 'convert "$0" "${0%.*}.jpg"'

The one I use. It uses GNU Parallel to run multiple jobs at once, giving you a performance boost. It is installed by default on many systems and is almost definitely in your repo (it is a good program to have around).

ls -1 *.png | parallel convert '{}' '{.}.jpg'

The number of jobs defaults to the number of processes you have. I found better CPU usage using 3 jobs on my dual-core system.

ls -1 *.png | parallel -j 3 convert '{}' '{.}.jpg'

And if you want some stats (an ETA, jobs completed, average time per job...)

ls -1 *.png | parallel --eta convert '{}' '{.}.jpg'

There is also an alternative syntax if you are using GNU Parallel.

parallel convert '{}' '{.}.jpg' ::: *.png

And a similar syntax for some other versions (including debian).

parallel convert '{}' '{.}.jpg' -- *.png


#!/bin/bash
echo "This will resize all jpg images in the current directory to 640x480 (or 480x640, as appropriate), an optional paramter will be prefixed to result files."
for i in *.jpg ;
do
SIZEX=$(identify $i | egrep -o '[0-9]+x' | egrep -o '[0-9]+')
SIZEY=$(identify $i | egrep -o 'x[0-9]+' | egrep -o '[0-9]+')
if(test $SIZEX -ge $SIZEY;)then
  convert -size 640x480 $i -resize 640x480 $1_$i;
else
  convert -size 480x640 $i -resize 480x640 $1_$i;
fi
echo "resized image $1_$i created." ;
done