-
Notifications
You must be signed in to change notification settings - Fork 33
/
sharp_2x_zoom.pl
executable file
·138 lines (106 loc) · 2.91 KB
/
sharp_2x_zoom.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 31 October 2015
# Website: https://github.com/trizen
# Zoom a picture two times, without loosing too much details.
# Requires: wkhtmltoimage
use 5.010;
use strict;
use autodie;
use warnings;
use GD qw();
use File::Temp qw(tempfile);
use HTML::Entities qw(encode_entities);
GD::Image->trueColor(1);
sub help {
my ($code) = @_;
print <<"HELP";
usage: $0 [input image] [output image]
HELP
exit($code);
}
sub enhance_img {
my ($image, $out) = @_;
my $img = GD::Image->new($image) // return;
my ($width, $height) = $img->getBounds;
my $scale_width = 2 * $width;
my $scale_height = $height;
my $resized = GD::Image->new($scale_width, $scale_height);
$resized->copyResampled($img, 0, 0, 0, 0, $scale_width, $scale_height, $width, $height);
($width, $height) = ($scale_width, $scale_height);
$img = $resized;
my @pixels;
foreach my $y (0 .. $height - 1) {
foreach my $x (0 .. $width - 1) {
my $index = $img->getPixel($x, $y);
push @pixels, [$img->rgb($index)];
}
}
my $header = <<"EOT";
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>${\encode_entities($image)}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type="text/css">
/*<![CDATA[*/
<!--
pre {
font-size: 1;
font-family: monospace;
}
EOT
my $footer = <<'EOT';
</pre></body></html>
EOT
my %colors;
my $style = '';
my @html;
my $name = 'A';
while (@pixels) {
push @html, [
map {
my $color = sprintf("%02x%02x%02x", @{$_});
if (not exists $colors{$color}) {
$colors{$color} = $name;
$style .= ".$name\{background-color:#$color;}\n";
$name++;
}
$colors{$color};
} splice(@pixels, 0, $width)
];
}
my $html = '';
foreach my $row (@html) {
while (@{$row}) {
my $class = shift @{$row};
my $count = 1;
while (@{$row} and $row->[0] eq $class) {
++$count;
shift @{$row};
}
$html .= qq{<span class="$class">} . (' ' x $count) . "</span>";
}
$html .= '<br/>';
}
$style .= <<'EOT';
-->
/*]]>*/
</style>
</head>
<body>
<pre>
EOT
$html = join('', $header, $style, $html, $footer);
my ($fh, $tmpfile) = tempfile(UNLINK => 1, SUFFIX => '.html');
print $fh $html;
close $fh;
system(
'wkhtmltoimage', '--quality', '100', '--crop-h', $height * 2,
'--crop-w', $width, '--crop-x', '8', '--crop-y',
'8', '--transparent', '--quiet', $tmpfile, $out
);
}
my $img = $ARGV[0] // help(1);
my $out = $ARGV[1] // help(1);
enhance_img($img, $out);