minimal pod documentation
[minime.git] / termimg
1 #!/usr/bin/perl
2 use 5.014;
3 my ($file, $size) = @ARGV;
4
5 my @info = eval {
6         require Image::ExifTool;
7         my $exif = Image::ExifTool->new->ImageInfo($file, {
8                 CoordFormat => '%.5f',
9                 DateFormat => '%Y-%m-%d %H:%M:%S',
10                 Exclude => 'System:*',
11         });
12         die "exiftool: $exif->{Error}\n" if $exif->{Error};
13         return (
14                 $exif->{MIMEType},
15                 $exif->{ImageSize} ? "$exif->{ImageSize} ($exif->{Megapixels}MP)" : (),
16                 join(' ',
17                         $exif->{YCbCrSubSampling} // (),
18                         $exif->{Interlace} // (),
19                         $exif->{Compression} // $exif->{FileType},
20                         $exif->{SVGVersion} // $exif->{PDFVersion} // (),
21                 ),
22                 $exif->{ColorType} ? "$exif->{BitDepth}bpp $exif->{ColorType}" : (),
23                 sprintf('exif (%d)', scalar %{$exif}),
24                 $exif->{Thumb} ? "thumb $exif->{ThumbnailLength}B" : (),
25                 (map "@ $_",
26                         $exif->{DateTimeOriginal} //
27                         $exif->{DateCreated} //
28                         $exif->{CircaDateCreated} //
29                         $exif->{ModifyDate} //
30                         $exif->{ZipModifyDate} //
31                         (),
32                 ),
33                 join(', ',
34                         $exif->{GPSPosition} // $exif->{Location} // (),
35                         $exif->{GPSAltitude} // (),
36                 ),
37                 (map "hw $_", join(' ',
38                         $exif->{Make} // (), $exif->{Model} // (),
39                         $exif->{FOV} ? "(FOV $exif->{FOV})" : (),
40                 ) || ()),
41                 (map "sw $_", $exif->{Software} // $exif->{Application} // ()),
42                 (map "> $_", $exif->{'Description-nl'} // ()),
43                 $exif->{Warning} ? "! $exif->{Warning}" : (),
44         );
45 };
46 warn $@ if $@;
47
48 my $filesize = (stat $file)[7];
49 eval {
50         require Digest::MD5;
51         open my $bin, '<', $file;
52         binmode $bin;
53         my $md5 = Digest::MD5->new->addfile($bin)->b64digest;
54         push @info, "# $md5 ($filesize)";
55 } or warn $@;
56
57 open my $pgm, '-|', convert => (
58         $file =>
59         -delete => '1--1',
60         '+distort' => SRT => '0,0 1,.56 0',
61         -thumbnail => $size || '40x12',
62         -colorspace => 'gray',
63         '-normalize',
64         -level => '-1%',
65         -channel => 'A', -threshold => '50%',
66         -background => 'black',
67         -layers => 'flatten',
68         -compress => 'none',
69         'pgm:-'
70 ) or die $!;
71
72 if (<$pgm> eq "P2\n") {
73         my ($width, $height) = split ' ', <$pgm>;
74         <$pgm>;  # ignore depth
75         my @ch = split //, " .:oO@";
76         while (<$pgm>) {
77                 print !$_ ? '/' : $ch[ $_ * @ch >> 8 ] for /\d+/g;
78                 print ' ', shift @info if @info;
79                 print $/;
80         }
81         substr $_, 0, 0, ' ' x ($width + 1) for @info;
82 }
83 say for @info;
84
85 =head1 NAME
86
87 termimg - media file fingerprint and metadata
88
89 =head1 SYNOPSIS
90
91 B<termimg> <file> [<size>]
92
93 =head1 DESCRIPTION
94
95 Identify given file by ascii image
96 with a summary of distinctive metadata.
97
98 =head1 AUTHOR
99
100 Mischa POSLAWSKY <perl@shiar.org>
101
102 =head1 LICENSE
103
104 AGPL3+