comparison of similar programs
[minime.git] / termimg
diff --git a/termimg b/termimg
index 4deee150e845031f965179380863084a65cab731..bd260c7d6f09a47ef89eabcaad61b275f8e6a686 100755 (executable)
--- a/termimg
+++ b/termimg
 #!/usr/bin/perl
 use 5.014;
-my ($file, $size) = @ARGV;
+
+our $VERSION = '1.00';
+
+use Getopt::Long '2.33', qw( :config gnu_getopt );
+GetOptions(\my %opt,
+       'size|s=s',
+);
+my ($file) = @ARGV;
+
+my @info = eval {
+       require Image::ExifTool;
+       my $exif = Image::ExifTool->new->ImageInfo($file, {
+               CoordFormat => '%.5f',
+               DateFormat => '%Y-%m-%d %H:%M:%S',
+               Exclude => 'System:*',
+       });
+       die "exiftool: $exif->{Error}\n" if $exif->{Error};
+       return (
+               $exif->{MIMEType},
+               $exif->{ImageSize} ? "$exif->{ImageSize} ($exif->{Megapixels}MP)" : (),
+               join(' ',
+                       $exif->{YCbCrSubSampling} // (),
+                       $exif->{Interlace} // (),
+                       $exif->{Compression} // $exif->{FileType},
+                       $exif->{SVGVersion} // $exif->{PDFVersion} // (),
+               ),
+               $exif->{ColorType} ? "$exif->{BitDepth}bpp $exif->{ColorType}" : (),
+               join(' ', 'exif',
+                       $exif->{ExifVersion},
+                       sprintf('(%d)', scalar %{$exif}),
+               ),
+               $exif->{Thumb} ? "thumb $exif->{ThumbnailLength}B" : (),
+               (map "@ $_",
+                       $exif->{DateTimeOriginal} //
+                       $exif->{DateCreated} //
+                       $exif->{CircaDateCreated} //
+                       $exif->{ModifyDate} //
+                       $exif->{ZipModifyDate} //
+                       (),
+               ),
+               join(', ',
+                       $exif->{GPSPosition} // $exif->{Location} // (),
+                       $exif->{GPSAltitude} // (),
+               ) || (),
+               (map "hw $_", join(' ',
+                       $exif->{Make} // (), $exif->{Model} // (),
+                       $exif->{FOV} ? "(FOV $exif->{FOV})" : (),
+               ) || ()),
+               (map "sw $_", $exif->{Software} // $exif->{Application} // ()),
+               (map "> $_", $exif->{'Description-nl'} // ()),
+               $exif->{Warning} ? "! $exif->{Warning}" : (),
+       );
+};
+warn $@ if $@;
+
+my $filesize = (stat $file)[7];
+eval {
+       require Digest::MD5;
+       open my $bin, '<', $file;
+       binmode $bin;
+       my $md5 = Digest::MD5->new->addfile($bin)->b64digest;
+       push @info, "# $md5 ($filesize)";
+} or warn $@;
+
+my $aspect = eval {
+       require Term::ReadKey;
+       my ($w, $h, $x, $y) = Term::ReadKey::GetTerminalSize();
+       return $x/$w / $y*$h;
+};
 
 open my $pgm, '-|', convert => (
+       $file =>
+       -delete => '1--1',
+       $aspect ? ('+distort' => SRT => "0,0 1,$aspect 0") : (),
+       -thumbnail => $opt{size} || '40x12',
+       -colorspace => 'gray',
+       '-normalize',
+       -level => '-1%',
+       -channel => 'A', -threshold => '50%',
+       -background => 'black',
+       -layers => 'flatten',
        -compress => 'none',
-       '+distort' => SRT => '0,0 1,.56 0',
-       -thumbnail => $size || '66x23',
-       $file => 'pgm:-'
+       'pgm:-'
 ) or die $!;
-<$pgm> eq "P2\n" or die "ascii pgm input required\n";
-<$pgm>; <$pgm>;  # ignore depth, dimensions
 
-my @ch = split //, " .:coO@";
-while (<$pgm>) {
-       print $ch[ $_ * @ch >> 8 ] for /\d+/g;
-       print $/;
+if (<$pgm> eq "P2\n") {
+       my ($width, $height) = split ' ', <$pgm>;
+       <$pgm>;  # ignore depth
+       my @ch = split //, " .:oO@";
+       while (<$pgm>) {
+               print !$_ ? '/' : $ch[ $_ * @ch >> 8 ] for /\d+/g;
+               print ' ', shift @info if @info;
+               print $/;
+       }
+       substr $_, 0, 0, ' ' x ($width + 1) for @info;
 }
+say for @info;
+
+__END__
+
+=head1 NAME
+
+termimg - media file fingerprint and metadata
+
+=head1 SYNOPSIS
+
+B<termimg> [<options>] <file>
+
+=head1 DESCRIPTION
+
+Identify given file by ascii image
+with a summary of distinctive metadata.
+
+=head1 OPTIONS
+
+=over
+
+=item -s, --size=<width>x<height>
+
+=back
+
+=head1 SEE ALSO
+
+          | output     | colors  | ff  | feats | time m-c | notes
+ ---------+------------+---------+-----+-------+----------|
+ jp2a     | ascii      | 1/2/16  | jpg | a hit |  125-241 |
+ img2txt  | ascii      | 256     | 5+  | adH t |      332 | caca-utils (imlib2)
+ catimg   | unicode    | rgb/256 | 3   |      v|      320 |
+ aview    | pager      | 1/2/3   | pnm |  dhit |  320+152 |
+ asciiart | ascii      | 1/16    | +   |   hi  |        ? |
+ termimg  | ascii      | 1       | +   | a   m | 1988+230 |
+ convert  | ascii (xpm)| 1       | +   | a  it | 1010     | imagemagick to xpm
+
+=head2 Features
+
+ a = aspect ratio option
+ d = color dithering
+ h = html export (H other formats)
+ i = invert option
+ m = metadata
+ t = gamma and/or brightness modification
+
+=head1 AUTHOR
+
+Mischa POSLAWSKY <perl@shiar.org>
+
+=head1 LICENSE
+
+AGPL3+