[Bins] Add images from gqview

Dragos Constantinescu d.constantinescu at tu-bs.de
Mon Feb 6 14:34:42 GMT 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Hi,

A new version of gqv2bins is attached.

Changes:

1. Since not all distributions provide the system command "mkdirhier",
    I replaced it with a perl routine.

2. The gqview keywords are now added to the field "keywords" in the XML
    image file. This enables the search using keywords. For this to work,
    "keywords" have to be added to the "searchFields" parameter in the
    bins configuration file and the key "keywords" should be added to the
    "getFields" function in bins source.

3. Very small changes in the manual page.


All the best,

________________________

         Dragos                                PGP key ID: A0887098

2006-02-06
2:29pm



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: For info see http://quantumlab.net/pine_privacy_guard/

iD8DBQFD516Cunb5IaCIcJgRAl3bAJwPHbCR9g5/zCHGl8nd4pC5bsmOiwCfeknB
LZfQf/KT9+TpVjXZ5tJBxwI=
=bIFb
-----END PGP SIGNATURE-----
-------------- next part --------------
#!/usr/bin/perl -w
use Getopt::Long;

$add='';
$remove='';
GetOptions ('add'    => \$add,
            'remove' => \$remove 
           );

$file = $ARGV[0];

die "please specify --add or --remove" unless ($add or $remove);


#### cange these according to your cfg ######
$imgdir='/space/dragos/poze/'; # this is your gqview directory tree base
$targetdir_base='/space/dragos/www/src-pcs/'; # image source directory for bins
$albumdir_base='/space/dragos/www/poze/'; # final location of the album
#############################################

#
# the key "keywords" should be added to the "getFields" function in bins source 
# in order to enable the search
# 

$file =~/$imgdir(.+\/)(.+?)$/;
$targetdir=$targetdir_base.$1;
$link_name=$targetdir.$2;
$albumfile=$albumdir_base.$1.$2;
$xml_name=$link_name.'.xml';
$albumfile=~/^(.+)(\..+?)$/;
@albumfiles=glob($1.'_*');
push @albumfiles, $albumfile.'.details.html';

%meta=read_meta($file); 
#$header=$meta{'header'}; 
@kw=@{$meta{'keywords'}}; $comment=$meta{'comment'};

print "$file:
       keywords: @kw
       comment:  $comment\n";

if ($add) {
  push @{$meta{'keywords'}}, 'bins';
  write_meta($file,\%meta);
  mkdirhier($targetdir);
  $targetdir=~s/ /\\ /g;$file=~s/ /\\ /g;$link_name=~s/ /\\ /g;
  system "chmod a+rx $targetdir
          ln -s $file $link_name
          /usr/local/bin/bins_edit --description \"$comment\" --generic keywords=\"@kw\" $link_name\n";
  print "image added to the album. 
  run bins $targetdir_base $albumdir_base \nto reflect the changes\n\n";
}

if ($remove) {
  print "removing link, xml, and album files\n";
  @{$meta{'keywords'}}=();
  foreach $kwrd (@kw) {push @{$meta{'keywords'}},$kwrd unless $kwrd eq 'bins'};
  write_meta($file,\%meta);
  unlink(@albumfiles,$link_name,$xml_name);
  print "run bins $targetdir_base $albumdir_base \nto reflect the changes\n\n";
}

sub read_meta {
  my $img=$_[0];
  $img=~/^(.+\/)(.+?)$/;
  my ($path,$imgname)=($1,$2);
  my $metafile=$path.".metadata/".$imgname.".meta";
  my $header=''; my @keywords=(); my $comment="";
  my %meta=(header => $header, keywords => \@keywords, comment => $comment);
  return %meta unless -r $metafile;

  my $field='';
  open(MFILE,"<$metafile");
  chomp($meta{'header'}=<MFILE>);
  while (<MFILE>){
    chomp(my $line=$_);
    next if $line eq '';
    if ($line=~/^\[(.+)\]$/){
      $field=$1;
      next;
    }
    next if $field eq '';
    last if $line =~/#end/;
    if ($field eq "keywords") {
      push @keywords,$line; 
    } elsif ($field eq "comment") {
      $meta{'comment'}.="$line\n";
    }
  }
  
  close MFILE;
  return %meta;
}

sub write_meta {
  my ($img,$meta) = @_;
  my $header=$meta{'header'};
  my @kw=unique(@{$meta{'keywords'}}); 
  my $comment=$meta{'comment'};
  $img=~/^(.+\/)(.+?)$/;
  my ($path,$imgname)=($1,$2);
  my $metafile=$path.".metadata/".$imgname.".meta";
  #$metafile='meta.test';
  $kwstr=join("\n", at kw);
  mkdir($path.'.metadata');
  open(MFILE,">$metafile");
   print MFILE "$header\n\n[keywords]\n$kwstr\n\n[comment]\n$comment\n#end\n";
  close MFILE;
}

sub unique {
  my %seen=(); my $item; my @uniq;
  foreach $item (@_) {
    push(@uniq, $item) unless $seen{$item}++;
  }
  return @uniq;
}

sub mkdirhier {
  my $dir=$_[0];
  my @components=split /\//, $dir;
  my $path="";
  if ($dir =~/^\//) {$path='/'; shift(@components);}
    foreach $component (@components) {
    $path.=$component.'/';
    mkdir($path) unless -e $path;
  }
}

__END__

=head1 NAME

gqv2bins

=head1 SYNOPSIS

B<gqv2bins> [ B<--add> | B<--remove> ] imagefile

=head1 DESCRIPTION

This script is useful if you have lots of pictures into a directory structure
and you want to add only some of them to a web album preserving the initial
structure.

To be used in conjunction with L<gqview(1)> and L<bins(1)>.
Change $imgdir, $targetdir_base and $albumdir_base according to your 
preferences. Add this script with appropriate options to 
Edit->Preferences->Editors in gqview:

For adding a picture to the album:
Menu name:      "Add to bins" 
Command line:   "gqv2bins -a %p"

For removing a picture from the album:
Menu name:      "Remove from bins" 
Command line:   "gqv2bins -r %p"

This way you can add ar remove one file (or all the selected files) with one 
click directly from gqview. 

=head1 OPTIONS

=over

=item B<--add> 

Add an image to the source directory to be used with bins: 
Links the argument (image under $imgdir) into the mirror directory structure
under $targetdir_base. Also sets the bins description field of the linked image 
to the gqview comment of the original image and ads the keyword "bins" to the
gqview keywords. A new field named "keywords" is created in the xml file 
corresponding to the image and is set to the list of gqview keywords. Thus, the 
keywords can be used in searches from bins. Just add "keywords" in the 
"searchFields" parameter in binsrc.

Example: the image $imgdir/canon/2004/braunschweig/someimage.jpg will be linked
to $targetdir_base/canon/2004/braunschweig/someimage.jpg and the gqview comment
will be transfered to the description field in file 
$targetdir_base/canon/2004/braunschweig/someimage.jpg.xml. After you add all
your images to the album source you can create the album  with bins
$targetdir_base $albumdir_base.

=item B<--remove> 

Remove an image from the album:
Deletes the previously created link, corresponding xml file and the album
files. It also removes the "bins" keyword from the gqview keyword list.

Note: This action is potentially destructive. Please make sure you set 
$imgdir, $targetdir_base, and $albumdir_base to their correct values.

=back

=head1 COPYRIGHT

Copyright 2005, Dragos Constantinescu.  All rights reserved.  This program is 
free software. It may be used, redistributed, and/or modified under  the same
terms as Perl itself.

=head1 AUTHOR

Dragos Constantinescu, dragos at venus.nipne.ro

=cut



More information about the Bins mailing list