#!/bin/bash
##################################################
# Fichier : imagemagick_exemples.sh
# But :
# Détails :
#
####################fin_entete####################
##################################################
geany $0 &
exit 1
# annoter images pour diffuser sur télé
ls -1 *jpg | xargs -i convert {} -gravity south -pointsize 50 -fill black -annotate 0 {} {}
ls -1 *jpg | xargs -i convert {} -gravity south -pointsize 50 -fill white -annotate +2+2 {} {}
# ajouter un cadre à une photo avec image magick
convert 01-nq8.png -bordercolor teal -border 20 lac_cadre.jpg
# Ajouter un texte en haut à gauche sur plusieurs images
mogrify -fill white -pointsize 16 -annotate +10+26 'Copie Interdite' *.jpg
# tiré de http://www.quennec.fr/trucs-astuces/syst%C3%A8mes/gnulinux/commandes/multim%C3%A9dia/photo/imagemagick
# ajouter une bordure/contour noir(e)
convert 0.png -bordercolor "#000000" -border 100x100 0.png
# convertir des images jpeg en jpeg2000
# tests
for t in 64 128 256 512 1024
do
for i in *jpg
do
#~ convert $1 -define jp2:tilewidth=$t -define jp2:tileheight=$t $1.tilewidth$t.jp2
convert $i -define jp2:rate=0.01 -define jp2:tilewidth=$t -define jp2:tileheight=$t $i.tilewidth$t.r001.jp2
#~ convert $1 -define jp2:rate=0.1 -define jp2:tilewidth=$t -define jp2:tileheight=$t $1.tilewidth$t.r01.jp2
#~ convert $1 -define jp2:rate=0.05 -define jp2:tilewidth=$t -define jp2:tileheight=$t $1.tilewidth$t.r005.jp2
done
done
# grouper des fichiers jpg dans un même pdf
convert *.jpg fichier.pdf
# attention : très long ou peut échouer si 200 jpg par exemple
# tester sur 50 fichiers jpg d'abord
# extraire les images d'un pdf constitué de fichiers jpg
# A TESTER
# http://codetheory.in/convert-split-pdf-files-into-images-with-imagemagick-and-ghostscript/
convert file.pdf[5-10] images/image.png
# MIEUX mais long !
# https://stackoverflow.com/questions/6605006/convert-pdf-to-image-with-high-resolution
convert -density 300 -trim test.pdf -quality 100 test.jpg
# capture écran cisco netacad
time find ./ -type f -name "*.png" -exec convert -verbose {} -crop 1280x830+0+83 {}.jpg \;
# suite d'opérations pour corriger un scan
1. scanner avec raspberry en 200 ppp
pendant écoute radio par ex. avec touche 8
2. rotation horaire :
$convert -rotate 90 source.jpg etape1.jpg
3. tailler la partie intéressante de l image
$convert etape1.jpg -crop 1810x1520+440+60 etape2.jpg
4. éventuellement séparer les deux pages
copier + tailler
5. éventuellement augmenter le contraste avant le jpegoptim
ou inverser les couleurs (texte blanc sur fond noir)
6. réduire taille finale sans perte de détails :
$jpegoptim -m40 etape2.jpg
7. renommer avec l ancien nom
$mv etape2.jpg source.jpg
# rotation image horaire
convert -rotate 90 source.jpg dest.jpg
#################################
# ImageMagick exemples #
#################################
ImageMagick en ligne de commande
http://www.imagemagick.org/www/command-line-options.html
#############################################
# Code couleurs
#############################################
http://www.imagemagick.org/script/color.php?ImageMagick=sj5bnkmjkc3g7te8f3lb9mkv25
convert -background black -fill "rgb(211,211,211)" -size 640x480 -gravity center -pointsize 70 caption:"texthau" a.jpg ; eog a.jpg
#############################################
# Créer une image depuis une commande
#############################################
convert -background black \
-fill "rgb(211,211,211)" \
-size 1920x1080 \
-gravity center \
-pointsize 55 \
caption:"==================\n\
Espace freebox\n\
==================\n\
$(df -h /n/f | grep -v -E "^[^ ]" | awk '{print "libre : "$3", \noccupé : "$2}' | sed 's/G/ Go/g')\n\n\
($(date "+%A %e %h %Y à %kh%M"))" \
/n/f/Photos/1_ESPACE_LIBRE_FREEBOX_.jpg
#############################################
# Ajouter un texte à une image avec ImageMagick
#############################################
1. créer l overlay : convert -fill red -size 400x100 caption:'This is a caption' caption.png
2. composite caption.png image_source.jpg image_finale.jpg
#############################################
# Coller/aggréger/concaténer 3 images l'une sous l'autre
#############################################
convert -background black -fill grey -gravity center -size 640x133 caption:1 1.jpg
convert -background black -fill white -gravity center -size 640x133 caption:2 2.jpg
convert -background black -fill grey -gravity center -size 640x134 caption:3 3.jpg
montage 1.jpg 2.jpg 3.jpg -tile 1x3 -geometry +0+0 123.jpg
eog 123.jpg
# coller deux images en laissant la place pour deux autres
montage *png -tile 4x1 -geometry +111+0 -background lightblue *jpg
# https://superuser.com/questions/290656/combine-multiple-images-using-imagemagick
convert -append in-*.jpg out.jpg
In order to have specific files appended, or skip numbers instead of getting the full "glob", you can mention the input files explicitly and put the append command afterwards
convert in-1.jpg in-5.jpg in-N.jpg +append out-in1-plus-in5-and-inN.jpg
You can use -append (instead of +append) for vertical paste-up.
Or:
montage -mode concatenate -tile 1x in-*.jpg out.jpg
will also create a file out.jpg that contains a vertical concatenation of the source images.
#############################################
# Faire une mosaique d'images
#############################################
Avec les images jpg du dossier courant :
montage $(ls *jpg) -tile 10x30 -resize 50x50 -geometry +0+0 tout.jpg
montage $(find *jpg) -tile 10x30 -resize 50x50 -geometry +0+0 tout.jpg
Vrai resize avec étirement si nécessaire = ajouter \!) :
http://www.imagemagick.org/Usage/resize/
convert dragon.gif -resize 64x64\! exact_dragon.gif
convert terminal.gif -resize 64x64\! exact_terminal.gif
Ainsi l image originale va remplir tout le cadre
de la nouvelle image même si le ratio est différent.
Ex : une image de 1440x1080 de départ peut être redimensionnée
en vrai 1920x1080 grâce à la ligne
mkdir dossier_des_1920x1080 ; for i in $(ls *jpg) ; do convert $i -resize 1920x1080\! dossier_des_1920x1080/$i ; date ; done | nl ; r
#############################################
# Éclaircir des images scannées (en noir et blanc)
#############################################
# dossier courant
for i in ls *jpg; do convert $i -modulate 125,0 -contrast results/$i; done
# arborescence complète, semble ne pas fonctionner
find ./ -iname "*jpg" -print0 | xargs -0 convert -modulate 100,0 -contrast {}
find ./ -iname "*jpg" -print0 | xargs -0 convert -modulate 125,0 -contrast {}
###########################
# transition entre images #
# (animation) #
###########################
Voir l'excellent site Fred's ImageMagick Scripts
http://www.fmwconcepts.com/imagemagick/transitions/index.php
http://www.imagemagick.org/Usage/annotating/
et Overlaying Images
##########################
# superposer deux images #
##########################
Superposer hand_point.gif sur dragon.gif (résultat dans d.jpg) :
composite -geometry +0+0 hand_point.gif dragon.gif d.jpg
=> les coins gauches supérieurs coïncident
#################
# Annoter une image
#################
http://www.imagemagick.org/Usage/annotating/
pour annoter dans l image meme :
convert Rfond.jpg -fill white -undercolor '#00000080' -gravity Center -pointsize 50 -annotate +0+$i 'Marc\nLa Semaine' Rfond.jpg
convert anno_undercolor.jpg -fill white -undercolor '#00000080' -gravity South -annotate +0+5 'CentreBas' anno_undercolor.jpg
ls -1 *.jpg | xargs -i convert {} -fill white -undercolor '#00000080' -gravity South -annotate +0+5 {} {}
convert anno_undercolor.jpg -fill white -undercolor '#00000080' -gravity SouthWest -annotate +0+5 'BasGauche' anno_undercolor.jpg
convert anno_undercolor.jpg -fill white -undercolor '#00000080' -gravity SouthEast -annotate +0+5 'BasDroite' anno_undercolor.jpg
convert image.jpg -background black -fill white label:"texte bas de page" -gravity Center -append image.jpg
=> les dimensions de l image sont augmentées
#############################
# Tailler une image
#############################
si d.jpg fait 120x150
convert d.jpg -crop 110x140+0+0 e.jpg
=> taille une portion de 110x140 depuis le bord supérieur gauche
# En fournissant les coordonnées des coins haut-gauche et bas-droit :
xGauche=313
xDroite=1539
yHaut=139
yBas=1080
avant="$(date)"
largeur=$((xDroite-xGauche))
hauteur=$((yHaut-yBas))
ls -1 *jpg | xargs -i convert -crop ${largeur}x${hauteur}+${xGauche}+${yHaut} {} {}_CROP.jpg
echo "$avant"
date
#############################
# Enlever le haut d'une image
#############################
si d.jpg fait 120x150
convert d.jpg -crop 120x140+0+10 e.jpg
=> enlève une bande de 10 pixels au-dessus
#############################
# Ajouter une bande noire en bas
#############################
si d.jpg fait 120x150
convert d.jpg -crop 120x140+0+10 -bordercolor darkgrey -border 10 -crop 120x150+10+20 e.jpg
=> ajout d'une bande gris foncé de 10 pixels en bas
#############################
# Réduire la taille d'une image
#############################
convert source.jpg -resize 300x300 dest.jpg
#############################
# Informations sur une image
#############################
identify image.jpg
convert -verbose dragon.gif /dev/null
=>
dragon.gif GIF 120x150 120x150+0+0 8-bit PseudoClass 256c 12KB 0.000u 0:00.009
dragon.gif=>/dev/null GIF 120x150 120x150+0+0 8-bit PseudoClass 256c 0.010u 0:00.000
##################################
# Centrer une image dans une image
# finale de taille fixe
##################################
Soit une image de départ de 301x300 pixels :
convert 301x300.jpg -resize 350x150 -gravity center -background red -extent 350x150 result.jpg
=> image sur fond rouge de 350x150 avec au centre l'image de départ réduite à 150x150 environ
convert 301x300.jpg -resize 50x200 -gravity center -background blue -extent 350x150 result2.jpg
=> image sur fond bleu de 350x150 avec au centre l'image de départ réduite à 50x50 environ
##################################
# Redimensionner une image
##################################
Voir le script video_d_images.sh
convert "$i" -gravity center -background $couleur_fond -resize $convresize -extent $convextent tmp.jpg 2>$logerreurs.2
# si la conversion échoue, le fichier 2>$logerreurs.2 n'est pas nul
# le tester avec if [ -s $logerreurs.2 ] ; then
Soit une image de 640x480 points
on veut qu au départ, elle ait une dimension de 320x240
au milieu d un fond de 640x480, à la fin elle remplit le cadre
convert paysage.jpg -gravity center -background red -resize 320x240 -extent 640x480 tmp01.jpg
=> l image d'origine est réduite à 320x240, au milieu d'une image de 640x480, sur fond rouge
##################################
# Dessiner quadrillage abscisses/ordonnées
##################################
xmax=$(identify $1 | cut -f3 -d' ' | cut -f1 -d'x')
ymax=$(identify $1 | cut -f3 -d' ' | cut -f2 -d'x')
for x in $(seq 100 100 $xmax) ; do xseq="$xseq -draw 'line $x,0 $x,$ymax' " ; done
for y in $(seq 100 100 $ymax) ; do yseq="$yseq -draw 'line 0,$y $xmax,$y' " ; done
echo "$xseq" > test.txt
echo "$yseq" >> test.txt
echo convert $1 -fill black "$xseq" "$yseq" $1.dest.jpg > quadriller.sh
chmod u+x quadriller.sh
./quadriller.sh
#~ convert origine.png -fill blue \
#~ -draw 'line 100,0 100,900' -draw 'line 200,0 200,900' -draw 'line 300,0 300,900' \
#~ dest.jpg
##################################
# Compression aller-retour
# png-gif-png
##################################
Source : fichier conversion_image_png_gif_png_meilleure_compression.txt
conversion_image_png_gif_png_meilleure_compression
Chose remarquable découverte par hasard :
Les captures d écran (comportant beaucoup de texte) au format png
prennant moins de place sur disque une fois converties en gif.
Et surprise, ces mêmes images gif reconverties au format png
prennent encore moins de place que les premières images png.
Exemple :
le dossier /tmp/images/1.png contient 18 images de 4,8 Mo au total
cd /tmp/images/1.png
mkdir ../2.gif
for i in $(ls -1 *png) ; do convert $i ../2.gif/${i%%png}gif ; done
le dossier /tmp/images/2.gif contient 18 images de 3,6 Mo au total
Gain : 25% p/p au début
cd /tmp/images/2.gif
mkdir ../3.png
for i in $(ls -1 *gif) ; do convert $i ../3.png/${i%%gif}png ; done
le dossier /tmp/images/3.png contient 18 images de 2,9 Mo au total
Gain : 40% p/p au début
##################################
# Créer animation depuis images
##################################
convert -delay 20 -loop 0 *.JPG animation.gif
delay 20 mets 20/100 de seconde entre chaque image et loop 0 fait tourner à l infini.
Pour faire un film MPEG au lieu d’un GIF animé ?
convert -delay 20 -loop 0 *.JPG animation.mpg
Pour faire une image animé MNG [2] au lieu d un GIF animé ?
convert -delay 20 -loop 0 *.JPG animation.mng
#####################################
# Batch resize images on Ubuntu Linux
#####################################
http://baptiste-wicht.com/posts/2010/09/tip-batch-resize-images-on-ubuntu-linux.html
And then, you can resize all the JPG images to a width of 640px
of the current folder using the single command :
mogrify -resize 640 *.jpg
If you want the height, just add a x :
mogrify -resize x640 *.jpg
You can also specify maximum width and height,
that can be useful if you have big images and
you don t want a width larger than x and a
height larger than y but you don t want to resize
little images in the same folder. Here is an example
resizing images if the width is larger than 1280 or
height larger than 1024 :
mogrify -resize '1280x1024>' *.jpg
##########################################################################
# To change all JP2 files to JPG format while retaining the original files
##########################################################################
mogrify -format jpg *.jp2
##########################################################################
# Redimensionner toutes les images à la même dimension
# pour créer une vidéo avec ffmpeg ou mencoder
##########################################################################
mogrify -resize 800x600 -background black -gravity center -extent 800x600 -format jpg -quality 75 *