ef57cd3470bde52cede38cf8c1fefbe409eef8ca
[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         });
11         die "exiftool: $exif->{Error}\n" if $exif->{Error};
12         return (
13                 $exif->{MIMEType},
14                 $exif->{ImageSize} ? "$exif->{ImageSize} ($exif->{Megapixels}MP)" : (),
15                 join(' ',
16                         $exif->{YCbCrSubSampling} // (),
17                         $exif->{Interlace} // (),
18                         $exif->{Compression} // $exif->{FileType},
19                         $exif->{SVGVersion} // $exif->{PDFVersion} // (),
20                 ),
21                 $exif->{ColorType} ? "$exif->{BitDepth}bpp $exif->{ColorType}" : (),
22                 sprintf('exif x%d', scalar %{$exif}),
23                 $exif->{Thumb} ? "thumb $exif->{ThumbnailLength}B" : (),
24                 (map "@ $_",
25                         $exif->{DateTimeOriginal} //
26                         $exif->{DateCreated} //
27                         $exif->{CircaDateCreated} //
28                         $exif->{ModifyDate} //
29                         $exif->{ZipModifyDate} //
30                         (),
31                 ),
32                 join(', ',
33                         $exif->{GPSPosition} // $exif->{Location} // (),
34                         $exif->{GPSAltitude} // (),
35                 ),
36                 (map "hw $_", join(' ',
37                         $exif->{Make} // (), $exif->{Model} // (),
38                         $exif->{FOV} ? "(FOV $exif->{FOV})" : (),
39                 ) || ()),
40                 (map "sw $_", $exif->{Software} // $exif->{Application} // ()),
41                 (map "> $_", $exif->{'Description-nl'} // ()),
42                 $exif->{Warning} ? "! $exif->{Warning}" : (),
43         );
44 };
45 warn $@ if $@;
46
47 my $filesize = (stat $file)[7];
48 eval {
49         require Digest::MD5;
50         open my $bin, '<', $file;
51         binmode $bin;
52         my $md5 = Digest::MD5->new->addfile($bin)->b64digest;
53         push @info, "# $md5 ($filesize)";
54 } or warn $@;
55
56 open my $pgm, '-|', convert => (
57         $file =>
58         -delete => '1--1',
59         '+distort' => SRT => '0,0 1,.56 0',
60         -thumbnail => $size || '40x12',
61         -colorspace => 'gray',
62         '-normalize',
63         -background => 'black',
64         -layers => 'flatten',
65         -compress => 'none',
66         'pgm:-'
67 ) or die $!;
68
69 if (<$pgm> eq "P2\n") {
70         my ($width, $height) = split ' ', <$pgm>;
71         <$pgm>;  # ignore depth
72         my @ch = split //, " .:oO@";
73         while (<$pgm>) {
74                 print $ch[ $_ * @ch >> 8 ] for /\d+/g;
75                 print ' ', shift @info if @info;
76                 print $/;
77         }
78         substr $_, 0, 0, ' ' x ($width + 1) for @info;
79 }
80 say for @info;