image2matrix.pl 673 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 13 August 2016
  5. # Website: https://github.com/trizen
  6. # Transform an image into a matrix of RGB values.
  7. use 5.010;
  8. use strict;
  9. use warnings;
  10. use Imager;
  11. my $file = shift(@ARGV) // die "usage: $0 [image]";
  12. my $img = Imager->new(file => $file);
  13. foreach my $y (0 .. $img->getheight - 1) {
  14. say join(
  15. ',',
  16. map {
  17. my $color = $img->getpixel(y => $y, x => $_);
  18. my ($r, $g, $b) = $color->rgba;
  19. my $rgb = $r;
  20. $rgb = ($rgb << 8) + $g;
  21. $rgb = ($rgb << 8) + $b;
  22. $rgb
  23. } (0 .. $img->getwidth - 1)
  24. );
  25. }