Skip to content

Instantly share code, notes, and snippets.

@shoorick
Last active November 15, 2023 21:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoorick/483212 to your computer and use it in GitHub Desktop.
Save shoorick/483212 to your computer and use it in GitHub Desktop.
Moves images from memory card into specified directory and rename these images
!Build/
.last_cover_stats
/META.yml
/META.json
/MYMETA.*
*.o
*.pm.tdy
*.bs
# Devel::Cover
cover_db/
# Devel::NYTProf
nytprof.out
# Dizt::Zilla
/.build/
# Module::Build
_build/
Build
Build.bat
# Module::Install
inc/
# ExtUtils::MakeMaker
/blib/
/_eumm/
/*.gz
/Makefile
/Makefile.old
/MANIFEST.bak
/pm_to_blib
/*.zip
# Komodo IDE
*.komodoproject
.komodotools

move-images

Move images and group them together

Description

Move images from removable memory card to HDD:

  • remove leading letters from its names,
  • lowercase these names,
  • attempt to create subfolder named as year/month/day
  • move file into this subfolder
  • and then change file mode of moved file.

Usage

./move-images.pl [ options ] [ path-to-memory-card ]

Options

-s, --src, --source=PATH

Use PATH for source instead of (first-argument)/DCIM

-d, --dst, --destination=PATH

Use PATH for destination instead of ~/photo/*

-p, --precision=LEVEL

Set precision for grouping of photos. Allowed values are from 0 for year through default value 2 for day up to 5 for second.

--chmod=MODE

Change mode of processed files to MODE.

-k, --keep-prefix

Do not remove non-digital prefixes from names of files.

-?, -h, --help

Print a brief help message and exit.

-m, --man, --manual

Prints the manual page and exit.

-v, --verbose

Be verbose. Show names of processed files.

Author

Alexander Sapozhnikov http://shoorick.ru/ shoorick@cpan.org

License

This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.

See also

https://github.com/shoorick/move-images

#!/usr/bin/perl -wl
=head1 NAME
move-images - move images and group them together.
=head1 DESCRIPTION
Move images from memory card to HDD:
remove leading letters from its names, lowercase these names,
attempt to create subfolder named as I<year>/I<month>/I<day>
and move file into this subfolder and change file mode of moved file.
=head1 USAGE
./move-images [ options ] [ path-to-memory-card ]
=head1 OPTIONS
=over 4
=item B<-s>, B<--src>, B<--source>=C<PATH>
Use C<PATH> for source instead of (first-argument)/DCIM
=item B<-d>, B<--dst>, B<--destination>=C<PATH>
Use C<PATH> for destination instead of ~/photo/*
=item B<-p>, B<--precision>=C<LEVEL>
Set precision for grouping of photos.
Allowed values are from C<0> for year
through default value C<2> for day up to C<5> for second.
=item B<--chmod>=C<MODE>
Change mode of processed files to C<MODE>.
=item B<-k>, B<--keep-prefix>
Do not remove non-digital prefixes from names of files.
=item B<-?>, B<-h>, B<--help>
Print a brief help message and exit.
=item B<-m>, B<--man>, B<--manual>
Prints the manual page and exit.
=item B<-v>, B<--verbose>
Be verbose. Show names of processed files.
=back
=head1 LEGACY TUNING
Tuning by source code editing is B<deprecated> now.
Use options instead (see above).
=head1 AUTHOR
Alexander Sapozhnikov
L<< http://shoorick.ru/ >>
L<< E<lt>shoorick@cpan.orgE<gt> >>
=head1 LICENSE
This program is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
use strict;
use File::Find;
use File::Path qw(make_path);
use File::Copy;
use Getopt::Long;
use Image::ExifTool qw(:Public);
use Desktop::Notify;
map { $_ = '' } my (
$need_help, $need_manual, $verbose,
$path_src, $keep_prefix,
);
my $path_dst = $ENV{'HOME'} . '/photo'; # path to destination. Don't use ~ for your homedir
my $precision = 2; # 0 for year .. 5 for second
my $mode = 0644; # for chmod
GetOptions(
'help|?' => \$need_help,
'manual' => \$need_manual,
'verbose' => \$verbose,
'precision:i' => \$precision,
'source|src:s' => \$path_src,
'destination|dst:s' => \$path_dst,
'chmod:s' => \$mode,
'keep_prefix' => \$keep_prefix,
);
use Pod::Usage qw( pod2usage );
pod2usage('verbose' => 2)
if $need_manual;
# print help message when required arguments are omitted
pod2usage(1)
if $need_help;
unless ( $path_src ) {
$path_src = shift @ARGV || '/media/nikon'; # path to memory card
$path_src .= '/DCIM';
}
find( \&wanted, $path_src );
# Say when ended
my $summary = 'Photos was moved';
my $message = 'You can unmount your memory card';
if ( $ENV{'DISPLAY'} ) {
my $notify = Desktop::Notify->new();
my $notification = $notify->create(
'summary' => $summary,
'body' => $message,
'timeout' => 5000,
);
$notification->show();
$notification->close();
}
else {
print STDERR "$summary. $message." if $verbose;
}
exit;
sub wanted {
return unless /\.(3gp|avi|cr2|crw|dng|jpe?g|mov|nef|raf|raw|tiff?)/i;
my $new_name = lc $_;
$new_name =~ s/^\D+// unless $keep_prefix;
my $info = ImageInfo( $File::Find::name );
print STDERR "$info->{'Error'} - $info->{'Directory'}/$info->{'FileName'}"
if exists $info->{'Error'};
return unless $info->{'DateTimeOriginal'};
my @date = split /\D+/, $info->{'DateTimeOriginal'};
$#date = $precision;
my $new_dir = join '/', $path_dst, @date;
make_path $new_dir
unless -d $new_dir;
my $new_path = "$new_dir/$new_name";
-d $new_dir
and move $File::Find::name, $new_path
and chmod $mode, $new_path
and $verbose and print STDERR "$File::Find::name => $new_path";
} # sub wanted
@shoorick
Copy link
Author

Documentation in Russian

Описание

Перемещает картинки между каталогами (например, с карты памяти на жёсткий диск):

  • создаёт подкаталоги с именами год/месяц/день (глубину можно задать произвольно, вплоть до секунд).
  • раскладывает картинки в соответствующие подкаталоги, беря дату из EXIF.
  • убирает (если не задано иное) из имён файлов ведущие не-цифры (обычно буквы, например, DSC), имена файлов приводит к нижнему регистру.
  • устанавливает заданный режим для файла (имеет смысл, когда на карточке с файловой системой FAT32 все файлы являются исполнимыми).

Использование

Программа запускается с необязательными параметрами, в том числе путём к исходному каталогу, откуда надо забрать файлы:

./move-images [ параметры ] [ путь-к-каталогу ]

При монтировании карты памяти в оконной среде GNOME можно запустить эту программу — GNOME передаст в программу путь к точке монтирования в качестве параметра.

Параметры

  • -s, --src, --source=ПУТЬ

    Копировать файлы из указанного ПУТИ вместо (первый-аргумент)/DCIM

  • -d, --dst, --destination=ПУТЬ

    Копировать файлы в папку с указанным ПУТЁМ, а не в ~/photo/*

  • -p, --precision=ТОЧНОСТЬ

    Задаёт точность группировки скопированных файлов.
    Допустимые значения — от 0 (все файлы одного года помещаются в общую папку) через 2 (по папке для каждого дня — значение по умолчанию) до 5 (папка для каждой секунды).

  • --chmod=ПРАВА

    Задаёт ПРАВА копируемым файлам. ПРАВА указываются в восьмеричном виде с ведущим нулём (например, 0644 соответствует -rw-r--r--).

  • -k, --keep-prefix

    Не удалять нецифровые префиксы имён файлов.

  • -?, -h, --help

    Выводит краткую справку и завершает работу.

  • -m, --man, --manual

    Выводит подробное руководство и завершает работу.

  • -v, --verbose

    Выводит имена обрабатываемых файлов. По умолчанию отключено.

Лицензия

Программа является свободным программным обеспечением, вы можете распространять или модифицировать её на тех же условиях, что и Perl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment