24f6a33613a6048d84af5cff5bdf996f97a4923f
[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         -background => 'black',
65         -layers => 'flatten',
66         -compress => 'none',
67         'pgm:-'
68 ) or die $!;
69
70 if (<$pgm> eq "P2\n") {
71         my ($width, $height) = split ' ', <$pgm>;
72         <$pgm>;  # ignore depth
73         my @ch = split //, " .:oO@";
74         while (<$pgm>) {
75                 print $ch[ $_ * @ch >> 8 ] for /\d+/g;
76                 print ' ', shift @info if @info;
77                 print $/;
78         }
79         substr $_, 0, 0, ' ' x ($width + 1) for @info;
80 }
81 say for @info;