cyan_vision.pl 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 16 November 2016
  5. # Website: https://github.com/trizen
  6. # Redraws each pixel as a cyan colored circle.
  7. # WARNING: this process is *very* slow for large images.
  8. use 5.010;
  9. use strict;
  10. use warnings;
  11. use Imager;
  12. use List::Util qw(max);
  13. my @matrix;
  14. {
  15. my $img = Imager->new(file => shift(@ARGV))
  16. || die die "usage: $0 [image]\n";
  17. my $height = $img->getheight - 1;
  18. my $width = $img->getwidth - 1;
  19. foreach my $y (0 .. $height) {
  20. push @matrix, [
  21. map {
  22. my ($r, $g, $b) = $img->getpixel(y => $y, x => $_)->rgba;
  23. my $rgb = $r;
  24. $rgb = ($rgb << 8) + $g;
  25. $rgb = ($rgb << 8) + $b;
  26. $rgb
  27. } (0 .. $width)
  28. ];
  29. }
  30. }
  31. my $max_color = 2**16 - 1; # normal color is: 2**24 - 1
  32. my $scale_factor = 3; # the scaling factor does not affect the performance
  33. my $radius = $scale_factor / atan2(0, -'inf');
  34. my $space = $radius / 2;
  35. my $img = Imager->new(
  36. xsize => @{$matrix[0]} * $scale_factor,
  37. ysize => @matrix * $scale_factor,
  38. channels => 3,
  39. );
  40. my $max = max(map { @$_ } @matrix);
  41. foreach my $i (0 .. $#matrix) {
  42. my $row = $matrix[$i];
  43. foreach my $j (0 .. $#{$row}) {
  44. $img->circle(
  45. r => $radius,
  46. x => $j * $scale_factor + $radius + $space,
  47. y => $i * $scale_factor + $radius + $space,
  48. color => sprintf("#%06x", $row->[$j] / $max * $max_color),
  49. );
  50. }
  51. }
  52. $img->write(file => 'cyan_image.png');