rgb_dump.pl 705 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/perl
  2. # Dump the first n pixels from a given image.
  3. use 5.020;
  4. use warnings;
  5. use Imager;
  6. use experimental qw(signatures);
  7. @ARGV || do {
  8. say STDERR "usage: $0 [input.png] [n]";
  9. exit(2);
  10. };
  11. my $in_file = $ARGV[0];
  12. my $n = $ARGV[1] // 10;
  13. my $img = 'Imager'->new(file => $in_file)
  14. or die "Can't read image: $in_file";
  15. my $width = $img->getwidth;
  16. my $height = $img->getheight;
  17. OUTER: foreach my $y (0 .. $height - 1) {
  18. foreach my $x (0 .. $width - 1) {
  19. --$n >= 0 or last OUTER;
  20. my $color = $img->getpixel(x => $x, y => $y);
  21. my ($r, $g, $b) = $color->rgba;
  22. printf("%08b,%08b,%08b | %2x,%2x,%2x | %3d,%3d,%3d\n", ($r, $g, $b) x 3);
  23. }
  24. }