\chapter{Image} \label{chap:image} Le traitement des images est un art à part entière. Nous allons voir quelques grands classiques de la catégorie pas du tout interactif. Le genre de machin que l'on peut ranger au fond d'un script shell pour le réutiliser la semaine prochaine. \begin{itemize} \item{ImageMagick} \item{Gmic} \item{NetPBM} \item{Portable Network Graphic} \item{TIFF} \end{itemize} % ------------------------------------------------------------------- \section{ImageMagick}\index{ImageMagick} Attention, ça va devenir \textsl{hardu}% \footnote{Nous attendons tous avec impatience l'ouvrage magistral de maitre Brunus}, l'abondance d'options des outils ImageMagick est vraiment énorme, et leurs interactions parfois troublantes\dots Il y a plusieurs binaires dans le paquet : \texttt{convert} pour la plupart des traitements, \texttt{display} et \texttt{animate} pour visualiser des images, \texttt{identify} pour en lire les méta-données. Ils partagent un certain nombre d'options communes. Certains d'entre eux peuvent avoir un effet destructif% \footnote{En plus d'avoir un nom de série $Z$.}, vous êtes prévenus. \subsection{L'option \texttt{-geometry}} C'est une notion importante, car elle est utilisée comme paramètre par beaucoup d'options. C'est avec ça que l'on peut définir des choses comme la taille (largeur et hauteur) d'une image, ou la position d'un texte dans l'image, ou une zone sur laquelle on va travailler. Et c'est assez compliqué, mais pas que. \begin{tabular}{|l|p{7cm}|} \hline Code & Signification \\ \hline scale\% & Height and width both scaled by specified percentage. \\ scale-x\%xscale-y\% & Height and width individually scaled by specified percentages. (Only one \% symbol needed.) \\ width & Width given, height automagically selected to preserve aspect ratio. \\ xheight & Height given, width automagically selected to preserve aspect ratio. \\ widthxheight & Maximum values of height and width given, aspect ratio preserved. \\ widthxheight\^ & Minimum values of width and height given, aspect ratio preserved. \\ widthxheight! & Width and height emphatically given, original aspect ratio ignored. \\ widthxheight> & Shrinks an image with dimension(s) larger than the corresponding width and/or height argument(s). \\ widthxheight< & Enlarges an image with dimension(s) smaller than the corresponding width and/or height argument(s). \\ area@ & Resize image to have specified area in pixels. Aspect ratio is preserved. \\ x:y & Here x and y denotes an aspect ratio (e.g. 3:2 = 1.5). \\ \hline \end{tabular} % !!! % est-il nécessaire de traduire ce tableau ? \subsection{Écrire du texte} Voici un exemple concret (tiré du script d'encodage du \textsc{cloître}\index{cloître}) qui montre l'essentiel pour bien débuter. C'est une fonction écrite en bash\index{bash} qui rajoute un texte sur une image~: \lstinputlisting[language=sh]{code/tagpic.sh} Une petite commande toute bête pour rajouter la date facilement à une photo d’après les données EXIF\index{EXIF}~: \begin{lstlisting} convert nom_du_fichier_source.jpg -gravity SouthEast \ -font Fudd-Regular -pointsize 40 -fill white \ -annotate +20+20 "%[exif:DateTimeOriginal]" \ -quality 100 nom_du_fichier_destination.jpg \end{lstlisting} Quelques explications semblent nécessaires, parce que certaines options sont un peu ésotériques et parfois inter-dépendantes\dots \begin{tabular}{|l|p{7cm}|} \hline Code & Signification \\ \hline txtfont & \\ fontsize & \\ kerning & espacement entre les caractères \\ color & \\ stroke & couleur 'bord' de caractère \\ strokewidth & largeur de la bordure \\ txtopts & \\ gravity & \\ annotate & \\ \hline \end{tabular} \subsection{Questions...} \begin{itemize} \item{Comment mettre une couleur de fond à ce texte ?} \end{itemize} \subsection{montage} \begin{quote} create a composite image by combining several separate images. The images are tiled on the composite image optionally adorned with a border, frame, image name, and more. \end{quote} Krkrkr\dots \subsection{Faire des GIFs animées}\index{GIF} Ces petites images clignotantes sont l'essence même du Web\index{web} moderne depuis 1992 malgré leurs limitations, et \texttt{convert} sait très bien les générer. Encore un grand merci à Compuserve\index{Compuserve} pour cette création essentielle à l'art graphique moderne. \begin{verbatim} convert -delay 20 -loop 0 a.png b.png c.png foo.gif \end{verbatim} Ce sujet pertinent est abordé plus en détails en page \pageref{chap:gif89a} % ------------------------------------------ \subsection{Extraire une partie de l'image} Dans cette fonctionnalité aussi, il semble y avoir quelques subtilités. \begin{verbatim} DIMENSION="1024x768" POSITION="+R12+0" convert -crop ${DIMENSION}${POSITION} +repage $img $dst \end{verbatim} \textsl{Use +repage to completely remove/reset the virtual canvas meta-data from the images.} En gros, on en a besoin quand une opération va changer la dimension de l'image. En détail, j'ai pas tout compris. % ------------------------------------------ \subsection{conversion vers TGA} \index{TGA} On trouve encore en circulation de très vieux logiciels qui ne savent lire qu'un encore plus ancien format : \textsl{Targa file format}. Et dans une variante bien précise. Il nous faut donc une incantation bien précise : \begin{lstlisting} GOTO_TGA () { convert $1 \ -compress none \ -alpha off \ -colorspace RGB \ -type truecolor \ $2 } \end{lstlisting} On peut aussi procéder autrement, avec les outils PNM\index{PNM} : \begin{verbatim} pngtopnm \$negatif | ppmtotga -rgb -norle > \$picz \end{verbatim} % ------------------------------------------ \subsection{Trucs} \begin{itemize} \item{convert -average frame*.png output.png} \end{itemize} % ------------------------------------------ \subsection{Ressources} Le futur livre de Brunus. Réclamez les prmiers drafts dans l'IRC\index{IRC} \texttt{freenode\#paulla} \textit{:)} \texttt{http://www.fmwconcepts.com/imagemagick/index.php} % https://legacy.imagemagick.org/Usage/resize/ % ------------------------------------------------------------------- \section{Gmic}\index{Gmic} \textsl{Perform generic image processing operations, through the G'MIC language interpreter. gmic: GREYC's Magic for Image Computing.} Gmic se base sur une grosse\footnote{énorme, même. dans les deux sens\dots} bibliothèque de fonctions écrite en \texttt{C++}\index{C++} dont l'utilisation est bien docummentée dans le livre ???\index{XXX} Gmic existe aussi en plugin pour Gimp\index{Gimp} avec une interface pleine de curseurs et de boutons, qui facilite (ou pas) l'exploration des possibles. % ------------------------------------------------------------------- \section{NetPBM}\index{netpbm}\label{netpbm} With the Netpbm file formats, it’s trivial to output pixels using nothing but text based IO% \footnote{https://www.vidarholen.net/contents/blog/?p=904}. \begin{verbatim} #!/bin/bash exec > my_image.ppm # All echo statements will write here echo "P3 250 250 255" # magic, width, height, max component value for ((y=0; y<250; y++)) { for ((x=0; x<250; x++)) { echo "$((x^y)) $((x^y)) $((x|y))" # r, g, b } } \end{verbatim} \subsection{Questions} Comment faire un \textsl{resize} avec NetPBM ? Est-il nécessaire d'apprendre la bibliothèque de fonctions \texttt{libnetpbm}\footnote{Réponse: oui.} ? % ------------------------------------------------------------------- \section{Portable Network Graphic}\index{PNG} Voici le problème du soir% \footnote{\textsl{trollday} Fri Nov 13 00:07:19 CET 2020} : j'utilise pour lire ce format de fichier la bibliothèque \texttt{pnglite} qui a de gros soucis avec beaucoup de ceux-ci, par exemple ceux qui sont en \texttt{sRGB} crées par des outils essentiels comme POVray\index{Povray}. Il serait bon de voir la \texttt{libpng} officielle. pngcrush ? optipng ? \texttt{pngtopnm \$png | pnmtopng > \$goodpng} % ------------------------------------------------------------------- \section{Tag Image File Format} \index{TIFF} \label{TIFF} \begin{quote} Yes, but tiffcp is by far the most significant case. And perhaps one of the most used of the tools. I may not be impartial on this, though :-) because I use it heavily. \end{quote} \begin{verbatim} tiffcp combines one or more files created according to the Tag Image File Format, Revision 6.0 into a single TIFF file. Because the output file may be compressed using a different algorithm than the input files, tiffcp is most often used to convert between different compres‐ sion schemes. \end{verbatim} % ------------------------------------------------------------------- \section{Cimg} \index{Cimg} \texttt{Cimg} est l'énorme bibliothèque de fonctions (écrites en \texttt{C++}\footnote{\textsc{wtf?}}) sur lesquelles et basé Gmic. C'est long à compiler, ça produit des binaires énormes, mais il semble bien que ça puisse faire des trucs géniaux. \subsection{\textsc{hello world}} \begin{lstlisting} #define cimg_use_png #include "CImg.h" using namespace cimg_library; #define BLUR 0.35 int main(int argc,char **argv) { if (3 != argc) { fprintf(stderr, \ "Usage: %s infile.png outfile.png\n", argv[0]); exit(1); } CImg imgIn (argv[1]); CImg imgOut = \ imgIn.get_norm().blur(BLUR).normalize(0, 255); imgOut.save(argv[2]); return 0; } \end{lstlisting} \subsection{Questions...} \begin{itemize} \item{Comment virer le canal alpha à l'enregistrement d'une image en PNG ?} \end{itemize} % -------------------------------------------------------------------