Skip to content

Instantly share code, notes, and snippets.

@thewhodidthis
Last active May 17, 2023 19:11
Show Gist options
  • Save thewhodidthis/ea2d0520762c7955d3beb6c2121a3663 to your computer and use it in GitHub Desktop.
Save thewhodidthis/ea2d0520762c7955d3beb6c2121a3663 to your computer and use it in GitHub Desktop.
Extremely random pixel sorted image maker
use POSIX;
use warnings;
use strict;
# https://en.wikipedia.org/wiki/HSL_and_HSV#To_RGB
# Call like so: perl -l c.pl 100 10 90
my $h = shift // 0;
my $s = shift // 0;
my $l = shift // 0;
$s /= 100;
$l /= 100;
my $c = (1 - abs((2 * $l) - 1)) * $s;
my $k = $h / 60;
my $x = $c * (1 - abs(($k % 2) - 1));
my $m = $l - ($c / 2);
my ($r1, $g1, $b1) = (0, 0, 0);
if ($k >= 0 && $k < 1) {
($r1, $g1, $b1) = ($c, $x, 0);
}
if ($k >= 1 && $k < 2) {
($r1, $g1, $b1) = ($x, $c, 0);
}
if ($k >= 2 && $k < 3) {
($r1, $g1, $b1) = (0, $c, $x);
}
if ($k >= 3 && $k < 4) {
($r1, $g1, $b1) = (0, $x, $c);
}
if ($k >= 4 && $k < 5) {
($r1, $g1, $b1) = ($x, 0, $c);
}
if ($k >= 5 && $k < 6) {
($r1, $g1, $b1) = ($c, 0, $x);
}
my $r2 = floor(($r1 + $m) * 255);
my $g2 = floor(($g1 + $m) * 255);
my $b2 = floor(($b1 + $m) * 255);
print "$r2 $g2 $b2";
# The filenaming timestamp
D?=$$(date +"%Y%m%d%s")
# Pix or bitmap dimensions
W?=80
H?=45
# How many numbers total?
S?=$$(bc <<< "3 * $W * $H")
# How many samples?
G?=$$(bc <<< "3 * $H")
# Out image dimensions
w?=800
h?=450
# Expect rainbow like output.
pixmap/sorted/c:
@list=`printf "%s 90 50\n" $$(jot -r $$(($W * $H)) 0 360)`; while read line; do perl -l c.pl $$line; done <<< "$$list" | sort -r -n -k2 | tr "\n" " " | xargs | awk '{print "P3\n$W $H\n255\n"$$0}' > $D.ppm
pixmap/sorted:
@seq $W | xargs -n1 -I{} printf "abs(%s%%255)\n" $$(hexdump -e '3/1 "%d ""\n"' -n $G /dev/urandom) | bc | xargs -n3 | sort -r -n -k1 | tr "\n" " " | xargs | awk '{print "P3\n$W $H\n255\n"$$0}' > $D.ppm
pixmap/sorted/b:
@printf "abs(%s%%255)\n" $$(hexdump -e '3/1 "%d ""\n"' -n $S /dev/urandom) | bc | xargs -n3 | sort -n -k3 | tr "\n" " " | xargs | awk '{print "P3\n$W $H\n255\n"$$0}' > "$$(date +"%Y%m%d%s").ppm"
pixmap:
@printf "abs(%s%%255)\n" $$(hexdump -e '3/1 "%d ""\n"' -n $S /dev/urandom) | bc | tr "\n" " " | xargs | awk '{print "P3\n$W $H\n255\n"$$0}' > "$$(date +"%Y%m%d%s").ppm"
# Expect TV static like output.
bitmap:
@seq $W | xargs -n1 -I{} printf "abs(%s%%2)\n" $$(hexdump -e '3/1 "%d ""\n"' -n $G /dev/urandom) | bc | xargs -n3 | tr "\n" " " | awk '{print "P1\n$W $H\n"$$0}' > $D.pbm
bitmap/sorted:
@seq $W | xargs -n1 -I{} printf "abs(%s%%2)\n" $$(hexdump -e '3/1 "%d ""\n"' -n $G /dev/urandom) | bc | xargs -n3 | sort -r -n | tr "\n" " " | awk '{print "P1\n$W $H\n"$$0}' > $D.pbm
# Shortcut aliases for upsizing source images on Darwin.
image/png/b/s: bitmap/sorted
sips -s format png $$(ls -tr | tail -n 1) -o $D.png && sips -z $h $w $D.png
image/gif/b: bitmap
sips -s format gif $$(ls -tr | tail -n 1) -o $D.gif && sips -z $h $w $D.gif
image/jpg/b: bitmap
sips -s formatOptions best -s format jpeg $$(ls -tr | tail -n 1) -o $D.jpg && sips -z $h $w $D.jpg
image/jpg: pixmap/sorted
sips -s formatOptions best -s format jpeg $$(ls -tr | tail -n 1) -o $D.jpg && sips -z $h $w $D.jpg
# Pull out width by height random triplets.
1:
@hexdump -e '3/1 "%d ""\n"' -n $S /dev/urandom
# Compile formula for keeping numbers in range.
2:
@printf "abs(%s%%255)\n" `make 1`
# Fold negative and 255 exceeding numbers back into range.
3:
@make 2 | bc
# Organize line by line output back into RGB triplets.
4:
@make 3 | xargs -n3
# Sort such that reddest RGB triplets go last.
5:
@make 4 | sort -n
# Collect all triplets into a single space separated block.
6:
@make 5 | tr "\n" " "
# Add PPM file header.
7:
@make 6 | awk '{print "P3\n$W $H\n255\n"$$0}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment