mirror_images.pl 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/perl
  2. # Author: Trizen
  3. # Date: 04 June 2024
  4. # https://github.com/trizen
  5. # Mirror a given list of images (horizontal flip).
  6. use 5.036;
  7. use Imager qw();
  8. use File::Find qw(find);
  9. use Getopt::Long qw(GetOptions);
  10. my $img_formats = '';
  11. my @img_formats = qw(
  12. jpeg
  13. jpg
  14. png
  15. );
  16. sub usage ($code) {
  17. local $" = ",";
  18. print <<"EOT";
  19. usage: $0 [options] [dirs | files]
  20. options:
  21. -f --formats=s,s : specify more image formats (default: @img_formats)
  22. example:
  23. perl $0 ~/Pictures
  24. EOT
  25. exit($code);
  26. }
  27. GetOptions('f|formats=s' => \$img_formats,
  28. 'help' => sub { usage(0) },)
  29. or die("Error in command line arguments");
  30. push @img_formats, map { quotemeta } split(/\s*,\s*/, $img_formats);
  31. my $img_formats_re = do {
  32. local $" = '|';
  33. qr/\.(@img_formats)\z/i;
  34. };
  35. sub mirror_image ($image) {
  36. my $img = Imager->new(file => $image) or do {
  37. warn "Failed to load <<$image>>: ", Imager->errstr();
  38. return;
  39. };
  40. $img->flip(dir => "h");
  41. $img->write(file => $image);
  42. }
  43. @ARGV || usage(1);
  44. find {
  45. no_chdir => 1,
  46. wanted => sub {
  47. (/$img_formats_re/o && -f) || return;
  48. say "Mirroring: $_";
  49. mirror_image($_);
  50. }
  51. } => @ARGV;