Skip to content

Commit 5ab79f0

Browse files
committed
new file: Image/image-unpack.pl
1 parent 66b2489 commit 5ab79f0

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

Image/image-unpack.pl

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/perl
2+
3+
# Author: Trizen
4+
# Date: 29 April 2025
5+
# https://github.com/trizen
6+
7+
# Extract the {R,G,B} channels of an image, as binary data.
8+
9+
use 5.036;
10+
use GD qw();
11+
use Getopt::Long qw(GetOptions);
12+
13+
binmode(STDOUT, ':raw');
14+
15+
GD::Image->trueColor(1);
16+
17+
my $size = 80;
18+
my $red = 0;
19+
my $green = 0;
20+
my $blue = 0;
21+
22+
sub help($code = 0) {
23+
print <<"HELP";
24+
usage: $0 [options] [files]
25+
26+
options:
27+
-w --width=i : resize image to this width (default: $size)
28+
-R --red : extract only the RED channel (default: $red)
29+
-G --green : extract only the GREEN channel (default: $green)
30+
-B --blue : extract only the BLUE channel (default: $blue)
31+
32+
example:
33+
perl $0 --width 200 --red image.png > red_channel.bin
34+
HELP
35+
exit($code);
36+
}
37+
38+
GetOptions(
39+
'w|width=s' => \$size,
40+
'R|red!' => \$red,
41+
'G|green!' => \$green,
42+
'B|blue!' => \$blue,
43+
'h|help' => sub { help(0) },
44+
)
45+
or die "Error in command-line arguments!";
46+
47+
sub img_unpack($image) {
48+
49+
my $img = GD::Image->new($image) // return;
50+
my ($width, $height) = $img->getBounds;
51+
52+
if ($size != 0) {
53+
my $scale_width = $size;
54+
my $scale_height = int($height / ($width / ($size / 2)));
55+
56+
my $resized = GD::Image->new($scale_width, $scale_height);
57+
$resized->copyResampled($img, 0, 0, 0, 0, $scale_width, $scale_height, $width, $height);
58+
59+
($width, $height) = ($scale_width, $scale_height);
60+
$img = $resized;
61+
}
62+
63+
my @values;
64+
65+
foreach my $y (0 .. $height - 1) {
66+
foreach my $x (0 .. $width - 1) {
67+
my $index = $img->getPixel($x, $y);
68+
my ($R, $G, $B) = $img->rgb($index);
69+
70+
if ($red) {
71+
push @values, $R;
72+
}
73+
if ($green) {
74+
push @values, $G;
75+
}
76+
if ($blue) {
77+
push @values, $B;
78+
}
79+
}
80+
}
81+
82+
my $output_width = $width * ($red + $green + $blue);
83+
return unpack("(A$output_width)*", pack('C*', @values));
84+
}
85+
86+
print for img_unpack($ARGV[0] // help(1));

Image/image2digits.pl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,18 @@ sub img2digits {
6666
$img = $resized;
6767
}
6868

69-
my $avg = 0;
70-
my @averages;
69+
my @values;
7170

7271
foreach my $y (0 .. $height - 1) {
7372
foreach my $x (0 .. $width - 1) {
7473
my $index = $img->getPixel($x, $y);
7574
my ($r, $g, $b) = $img->rgb($index);
7675
my $value = max($r, $g, $b);
77-
push @averages, $digits[map_value($value, 0, 255, 0, $#digits)];
76+
push @values, $digits[map_value($value, 0, 255, 0, $#digits)];
7877
}
7978
}
8079

81-
unpack("(A$width)*", join('', @averages));
80+
unpack("(A$width)*", join('', @values));
8281
}
8382

8483
say for img2digits($ARGV[0] // help(1));

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ A nice collection of day-to-day Perl scripts.
537537
* [Gd star trails](./Image/gd_star_trails.pl)
538538
* [Gif2webp](./Image/gif2webp.pl)
539539
* [Horizontal scrambler](./Image/horizontal_scrambler.pl)
540+
* [Image-unpack](./Image/image-unpack.pl)
540541
* [Image2ascii](./Image/image2ascii.pl)
541542
* [Image2audio](./Image/image2audio.pl)
542543
* [Image2digits](./Image/image2digits.pl)
@@ -1023,6 +1024,7 @@ A nice collection of day-to-day Perl scripts.
10231024
* [Partial sums of powerfree part](./Math/partial_sums_of_powerfree_part.pl)
10241025
* [Partial sums of prime bigomega function](./Math/partial_sums_of_prime_bigomega_function.pl)
10251026
* [Partial sums of prime omega function](./Math/partial_sums_of_prime_omega_function.pl)
1027+
* [Partial sums of sigma0 function](./Math/partial_sums_of_sigma0_function.pl)
10261028
* [Partial sums of sigma function](./Math/partial_sums_of_sigma_function.pl)
10271029
* [Partial sums of sigma function times k](./Math/partial_sums_of_sigma_function_times_k.pl)
10281030
* [Partial sums of sigma function times k to the m](./Math/partial_sums_of_sigma_function_times_k_to_the_m.pl)

0 commit comments

Comments
 (0)