#!/usr/bin/perl -w
#########################################################################################
#
# L'idée est de créer un fichier de définitions récurrentes qui permettra de construire
# un second fichier de définitions qu'il faudra au besoin éditer et compléter.
# Et enfin, la commande [ $ iptc_exiftool.pl -run ] communiquera tous les paramètres à
# [ exiftool -overwrite_original ].
#
# iptc_exiftool
# -h -H :
# Page d'aide
# -make_list :
# Création du fichier de définitions recurrentes [ def_macro.txt ] si il n'existe pas
# Création du fichier [ macro.txt ] d'après le fichier de description [ def_macro.txt ]
# -run :
# Lecture de [ macro.txt ] pour passage des paramètres à [ exiftool ]
#
#########################################################################################
#
# Script de gestion des tags IPTC pour les fichiers images
# Le script utilise EXIFTOOLS (libimage-exiftool-perl | perl-image-exiftool):
# $ exiftool -overwrite_original
# avec les paramètres définis depuis les fichiers
# [ def_macro.txt ]
# [ macro.txt ]
#
# Script cree par:
# -xcfaudio, b52
# http://www.xcfa.tuxfamily.org/
# -zarer http://www.road2mayotte.org/blook/
# http://www.road2mayotte.org/calepin/
# http://www.christophegallaire.org/photoblog/
#
# Licence GPL 3
#
#########################################################################################
#
# copyright : (C) 2012 by Claude Bulin
#
# iptc_exiftool is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# at your option) any later version.
#
# iptc_exiftool is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iptc_exiftoo. If not, see .
#
#########################################################################################
use strict; # Toutes les variables doivent etre declarees
use warnings; # Verification de la syntaxe
# use File::Copy; # Utilisation de la fonction copy
# use Switch; # Utilisation des switch
#
# TEST DE CHRISTOPHE:
#
# /usr/bin/iptc_exiftool.pl -make_list
# Can't locate Switch.pm in @INC (@INC contains: /usr/local/lib64/perl5
# /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl
# /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at
# /usr/bin/iptc_exiftool.pl line 52.
# BEGIN failed--compilation aborted at /usr/bin/iptc_exiftool.pl line 52.
#
# Je suis sous Fedora 17. Problème de paquets, non ?
my $VERSION = '0.0.9'; # VERSION DU LOGICIEL
my $PACKAGE = 'iptc_exiftool.pl'; # NOM DU LOGICIEL
#########################################################################################
# SI LE FICHIER CONTENU DANS '$_[0]' EST PRESENT RENVOIE 'TRUE' SINON RENVOIE 'FALSE'
#########################################################################################
sub le_fichier_est_present
{
if (-f "/sbin/$_[0]") {
return 'TRUE';
}
elsif (-f "/usr/bin/$_[0]") {
return 'TRUE';
}
elsif (-f "/usr/local/bin/$_[0]") {
return 'TRUE';
}
elsif (-f "/usr/sbin/$_[0]") {
return 'TRUE';
}
elsif (-f "/usr/bin/vendor_perl/$_[0]") {
return 'TRUE';
}
return 'FALSE';
}
#########################################################################################
# RECHERCHE DU TYPE DE CPU
#########################################################################################
my $TypeCpuIsAmd64 = (`uname -m` =~ "x86_64") ? "TRUE" : "FALSE";
my $MyCpuIs = (`uname -m`);
chomp($MyCpuIs);
#########################################################################################
# DETECTION SI MODE ADMINISTRATEUR
#########################################################################################
my $ModeAdministrateur = ($> != 0) ? "FALSE" : "TRUE";
#########################################################################################
# AFFICHAGE D'UN ENTETE
#########################################################################################
sub affiche_entete
{
print "\n";
print "+=============================================================================\n";
print "| $_[0]\n";
print "+=============================================================================\n";
}
#########################################################################################
# RAFFRAICHIR LA CONSOLE
#########################################################################################
system('clear');
#########################################################################################
# PRESENTAION DE LA BESTIOLLE
#########################################################################################
print " ______________________________________________________________________
| |
| $PACKAGE $VERSION |
|______________________________________________________________________|
| |
| Script de gestion des tags IPTC pour les fichiers images |
| Ce script utilise EXIFTOOL: |
| libimage-exiftool-perl | perl-image-exiftool |
| \$ exiftool -overwrite_original |
| avec les paramètres définis depuis les fichiers |
| [ def_macro.txt ] |
| [ macro.txt ] |
| |
| Script cree par: |
| -xcfaudio alias b52 |
| http://www.xcfa.tuxfamily.org/ |
| -zarer http://www.road2mayotte.org/blook/ |
| http://www.road2mayotte.org/calepin/ |
| |
| Licence GPL 3 |
|______________________________________________________________________|
| |","\n";
#########################################################################################
# ROOT ?
#########################################################################################
if ($ModeAdministrateur =~ "TRUE") {
print "| VOUS ETES EN MODE ADMINISTRATEUR |\n";
print "| [ $PACKAGE ] NE PEUT PAS ETRE UTILISE |\n";
} else {
print "| VOUS ETES EN MODE UTILISATEUR |\n";
print "| [ $PACKAGE ] peut être utilisé |\n";
}
print "|______________________________________________________________________|\n\n";
if ($ModeAdministrateur =~ "TRUE") {
exit 1;
}
#########################################################################################
# RECHERCHE DU PATHNAME ACTUEL
#########################################################################################
my $path_pwd = `pwd`; # LA PATHNAME ACTUEL
chomp($path_pwd);
my $path_file_iptc_def_macro_txt = $path_pwd . "/def_macro.txt"; # PATHNAME + def_macro.txt
my $path_file_iptc_macro_txt = $path_pwd . "/macro.txt"; # PATHNAME + macro.txt
my @tableau = (); # tableau pour memorisation de la base
#
# TEST DE CHRISTOPHE:
my $path_file_jpg = $path_pwd . "/ *.jpg *.JPG"; # PATHNAME DE RECHERCHE DES FICHIERS IMAGES AVEC glob()
#########################################################################################
# AFFICHAGE D'UN ENTETE
#########################################################################################
sub help
{
print "--- 1 ---\n";
print "[ \$ $PACKAGE -make_list ] \n";
print " Permet la création du fichier [ def_macro.txt ] si il n'existe pas.\n";
print " Les définition récurrentes de ce fichier doivent être complétées.\n";
print "\t-iptc:ObjectName=\"WRITE HERE YOUR DEFINITION\"\n";
print "\t-iptc:Keywords=\"WRITE HERE YOUR DEFINITION\"\n";
# print "\t-iptc:City=\"WRITE HERE YOUR DEFINITION\"\n";
# print "\t-iptc:Country-PrimaryLocationName=\"WRITE HERE YOUR DEFINITION\"\n";
# print "\t-iptc:Credit=\"Christophe Gallaire\"\n";
# print "\t-iptc:CopyrightNotice='Copyright Christophe Gallaire. Tous droits réservés.'\n";
# print "\t-iptc:Source=\"Christophe Gallaire\"\n";
# print "\t-iptc:Caption-Abstract=\"WRITE HERE YOUR DEFINITION\"\n";
# print "\t-imagedescription=\"WRITE HERE YOUR DEFINITION\"\n";
print "\t...\n";
print "--- 2 ---\n";
print "[ \$ $PACKAGE -make_list ] \n";
print " Lecture du fichier [ def_macro.txt ] et ...\n";
print " création du fichier [ macro.txt ] qui devra à son tour être complété.\n";
print " Exemple pour [ macro.txt ]:\n";
print "\tNAME_FILE_IMG=/home/toto/images/image_01.jpg\n";
print "\t-iptc:ObjectName=\"WRITE HERE YOUR DEFINITION\"\n";
print "\t-iptc:Keywords=\"WRITE HERE YOUR DEFINITION\"\n";
# print "\t-iptc:City=\"WRITE HERE YOUR DEFINITION\"\n";
# print "\t-iptc:Country-PrimaryLocationName=\"WRITE HERE YOUR DEFINITION\"\n";
# print "\t-iptc:Credit=\"Christophe Gallaire\"\n";
# print "\t-iptc:CopyrightNotice='Copyright Christophe Gallaire. Tous droits réservés.'\n";
# print "\t-iptc:Source=\"Christophe Gallaire\"\n";
# print "\t-iptc:Caption-Abstract=\"WRITE HERE YOUR DEFINITION\"\n";
# print "\t-imagedescription=\"WRITE HERE YOUR DEFINITION\"\n";
print "\t...\n";
print " La complétion des champs de [ macro.txt ] par vos soins pourra par exemple donner:\n";
print "\tNAME_FILE_IMG=/home/toto/images/image_01.jpg\n";
print "\t-iptc:ObjectName=\"Bidonville de Manga Télé\" \n";
print "\t-iptc:Keywords=\"Bidonville, Mayotte\"\n";
# print "\t-iptc:City=\"Mamoudzou\" \n";
# print "\t-iptc:Country-PrimaryLocationName=\"Mayotte, France\"\n";
# print "\t-iptc:Credit=\"Christophe Gallaire\"\n";
# print "\t-iptc:CopyrightNotice='Copyright Christophe Gallaire. Tous droits réservés.'\n";
# print "\t-iptc:Source=\"Christophe Gallaire\"\n";
# print "\t-iptc:Caption-Abstract=\"France - Bidonville de Manga Télé - Christophe Gallaire\" \n";
# print "\t-imagedescription=\"Enfants de Manga Télé\"\n";
print "\t...\n";
print "--- 3 ---\n";
print " Puis un appel au script avec le paramètre [ \$ $PACKAGE -run ] permettra à [ exiftool ] d'appliquer à chaque fichier image les données contenues dans la base\n";
print " Exemple:\n";
print "\t\$ exiftool -overwrite_original \\\n";
print "\t-iptc:ObjectName=\"Bidonville de Manga Télé\" \\\n";
print "\t-iptc:Keywords=\"Bidonville, Mayotte\"\\\n";
# print "\t-iptc:City=\"Mamoudzou\"\\\n";
# print "\t-iptc:Country-PrimaryLocationName=\"Mayotte, France\"\\\n";
# print "\t-iptc:Credit=\"Christophe Gallaire\"\\\n";
# print "\t-iptc:CopyrightNotice='Copyright Christophe Gallaire. Tous droits réservés.'\\\n";
# print "\t-iptc:Source=\"Christophe Gallaire\"\\\n";
# print "\t-iptc:Caption-Abstract=\"France - Bidonville de Manga Télé - Christophe Gallaire\"\\\n";
# print "\t-imagedescription=\"Enfants de Manga Télé\"\\\n";
print "\t/home/toto/images/image_01.jpg\n";
print "\n";
print "\n";
}
#########################################################################################
# CREATION DU FICHIER def_macro.txt
#########################################################################################
sub fonction_create_file_def
{
open SOURCE, "> $path_file_iptc_def_macro_txt";
print SOURCE "#\n";
print SOURCE "# def_macro.txt\n";
print SOURCE "#\n";
print SOURCE "# contient les definitions recurrentes a modifier\n";
print SOURCE "#\n";
print SOURCE "# AUCUN ESPACE ENTRE LE MOT CLE ET LA DEFINITION\n";
print SOURCE "#\n";
print SOURCE "-iptc:ObjectName=\"WRITE HERE YOUR DEFINITION\"\n";
print SOURCE "-iptc:Keywords=\"WRITE HERE YOUR DEFINITION\"\n";
print SOURCE "-iptc:City=\"WRITE HERE YOUR DEFINITION\" \n";
print SOURCE "-iptc:Country-PrimaryLocationName=\"WRITE HERE YOUR DEFINITION\"\n";
print SOURCE "-iptc:Credit=\"WRITE HERE YOUR DEFINITION\"\n";
print SOURCE "-iptc:CopyrightNotice='Copyright Christophe Gallaire. Tous droits réservés.'\n";
print SOURCE "-iptc:Source=\"Christophe Gallaire\"\n";
print SOURCE "-iptc:Caption-Abstract=\"WRITE HERE YOUR DEFINITION\"\n";
print SOURCE "# -iptc:Description=\"WRITE HERE YOUR DEFINITION\"\n";
print SOURCE "-imagedescription=\"WRITE HERE YOUR DEFINITION\"\n";
close SOURCE;
}
#########################################################################################
# CREATION DE LA BASE macro.txt DEPUIS LE FICHIER def_macro.txt
#########################################################################################
sub fonction_make_list
{
#########################################################################################
# TEST PRESENCE DU FICHIER: def_macro.txt
#########################################################################################
if ( ! -f "$path_file_iptc_def_macro_txt") {
fonction_create_file_def;
print "\n";
print "Création du fichier [ def_macro.txt ] contenant les définitions IPTC vient d'être créé\n";
print "---\n";
print ">>> LE FICHIER [ def_macro.txt ] EST PRET POUR ETRE MODIFIE <<<\n";
print "---\n";
print "Vous pourrez ensuite reutiliser la commande:\n";
print " \$ iptc_exiftool -make_list\n";
print "\n\n";
exit(1);
}
#########################################################################################
# LECTURE ET MEMORISATION DES DATAS DE: def_macro.txt
#########################################################################################
affiche_entete("LECTURE DU FICHIER: $path_file_iptc_def_macro_txt");
my @fields;
my $len;
my $len_scalar;
my $chaine;
my $mot_cle = "";
open F_LIRE, $path_file_iptc_def_macro_txt;
while(){
# PASSE SI COMMENTAIRE
next if ($_ =~ "#");
# DECOUPE LA LIGNE AVEC SPLIT POUR @fields
$chaine = $_;
chomp( $chaine );
@fields = split("",$chaine);
$len_scalar = scalar(@fields);
print "[LIGNE DU FICHIER] [$chaine]\n";
print "[DECOUPAGE LIGNE]\n";
$len = 0;
# PASSE SI LIGNE VIDE
next if ($len_scalar < 5);
# PASSE TOUS LES ESPACES DU DEBUT
while( $len < $len_scalar && "$fields[ $len ]" eq " " ) {
$len ++;
}
# MOT CLE ET DEFINITION
$mot_cle = "";
while( $len < $len_scalar ) {
$mot_cle = $mot_cle . $fields[ $len ];
$len ++;
}
print " $mot_cle\n";
push(@tableau, $mot_cle);
print "\n";
}
close(F_LIRE);
affiche_entete("DATAS EN TABLEAU DE $path_file_iptc_def_macro_txt");
foreach (@tableau ) {
print "$_\n";
}
#########################################################################################
# LECTURE DES ENTREES DU REPERTOIRE
#########################################################################################
affiche_entete("LECTURE DES NOMS DE FICHIERS JPG DEPUIS: $path_pwd");
my @include=glob("$path_file_jpg");
my $CptInclude = 0;
if( exists( $include[ 0 ] ) ) {
foreach (@include) {
if( $CptInclude > 0 ) {
printf ("\t%s \n",$_);
}
$CptInclude ++;
}
}
else {
print "\tLa recherche des fichiers images s'effectue avec\n";
print "\t$path_file_jpg\n";
print "\tLes extentions des fichiers ne correspondent pas aux criteres de recherche !!!\n\n";
exit(1);
}
affiche_entete("CREATION DU FICHIER $path_file_iptc_macro_txt");
open SOURCE, "> $path_file_iptc_macro_txt";
print SOURCE "#\n";
print SOURCE "# macro.txt\n";
print SOURCE "#\n";
print SOURCE "# contient les definitions a completer\n";
print SOURCE "#\n";
print SOURCE "# AUCUN ESPACE ENTRE LE MOT CLE ET LA DEFINITION\n";
print SOURCE "#\n";
$CptInclude = 0;
foreach (@include) {
# @include[ 0 ] contient le path et
# pas le nom d'un fichier !!!
if( $CptInclude > 0 ) {
foreach (@tableau ) {
print "\t$_\n";
print SOURCE "$_\n";
}
print "\tNAME_FILE_IMG=", $path_pwd, "/", $_, "\n";
print SOURCE "NAME_FILE_IMG=", $path_pwd, "/", $_, "\n";
print "\n";
print SOURCE "\n";
}
$CptInclude ++;
}
close SOURCE;
} # fonction_make_list
#########################################################################################
# LECTURE DES DATAS DU FICHIER macro.txt ET PASSAGE DES PAAMETRES à exiftool
#########################################################################################
sub fonction_apply_macro_txt
{
my @fields;
my $len;
my $len_scalar;
my $chaine;
my $mot_cle = "";
my $Cpt = 0;
my $string_call = "";
affiche_entete("LECTURE DE: $path_file_iptc_macro_txt");
#########################################################################################
# TEST PRESENCE DU FICHIER: macro.txt
#########################################################################################
if ( ! -f "$path_file_iptc_macro_txt") {
print "Le fichier [ $path_file_iptc_macro_txt ] n'existe pas !!!\n";
print "Veuillez recommencer ...\n";
print "\n\n";
exit 1;
}
open F_LIRE, $path_file_iptc_macro_txt;
while(){
# PASSE SI COMMENTAIRE
next if ($_ =~ "#");
# DECOUPE LA LIGNE AVEC SPLIT POUR @fields
$chaine = $_;
chomp( $chaine );
@fields = split("",$chaine);
$len_scalar = scalar(@fields);
next if ($len_scalar < 5);
$len = 0;
# PASSE TOUS LES ESPACES DU DEBUT
while( $len < $len_scalar && "$fields[ $len ]" eq " " ) {
$len ++;
}
# MOT CLE
$mot_cle = "";
while( $len < $len_scalar ) {
$mot_cle = $mot_cle . $fields[ $len ];
$len ++;
}
push(@tableau, $mot_cle);
}
close(F_LIRE);
affiche_entete("APPELS A: exiftool");
# CONSTRUCTION DE LA CHAINE DE PARAMETRES
$string_call = "exiftool -overwrite_original ";
for ($Cpt=0; $Cpt<@tableau; $Cpt++) {
if( $tableau[ $Cpt ] =~ "NAME_FILE_IMG=" ) {
# RECHERCHE ET REMPLACEMENT
$tableau[ $Cpt ]=~ s/NAME_FILE_IMG=//g;
# COMPLETER LA CHAINE DE PARAMETRES
$string_call = $string_call . $tableau[ $Cpt ];
print $string_call;
print "\n\n";
system( $string_call );
print "\n";
# RECONSTRUCTION DE LA CHAINE DE PARAMETRES
$string_call = "exiftool -overwrite_original ";
}
else {
# COMPLETER LA CHAINE DE PARAMETRES
$string_call = $string_call . "$tableau[ $Cpt ] ";
}
}
} # fonction_apply_macro_txt
#########################################################################################
# TEST LA PRESENCE DE: exiftool
#########################################################################################
if (&le_fichier_est_present("exiftool") =~ 'FALSE') {
affiche_entete("VEUILLEZ INSTALLER [ exiftool ] (libimage-exiftool-perl | perl-image-exiftool) qui est absent !!!");
print "\n\n";
exit 1;
}
#########################################################################################
# VERIFICATION DU NOMBRE DES ARGUMENTS EN LIGNE DE COMMANDE
#########################################################################################
if( @ARGV > 0 ) {
affiche_entete("LE PATHNAME EST: $path_pwd ");
}
elsif( @ARGV eq 0 ) {
help;
exit 1;
}
#########################################################################################
# EXAMINE LES PARAMETRES
#########################################################################################
my $argument = "";
foreach $argument (@ARGV) {
# ARGUMENTS EN MAJUSCULE
$argument = uc( $argument );
if( $argument eq "-H" || $argument eq "-HELP" ) {
affiche_entete("HELP");
help;
}
elsif( $argument eq "-MAKE_LIST" ) {
affiche_entete("-MAKE_LIST");
fonction_make_list;
}
elsif( $argument eq "-RUN" ) {
affiche_entete("RUN");
fonction_apply_macro_txt;
}
else {
print "PARAMETRE INCORRECT\n\n";
}
}
print "\n\n\n";
exit 0;