glitch20071120.pl


第1引数は画像ファイル、第2引数の等倍が壊すbyte。

usage

perl glitch20071120.pl ../Documents/icon/sakkaku.jpg 10000 > ./a.jpg

code

#!/usr/bin/perl
my ($source, $param) = @ARGV;

open my $image, '<', $source;
my $string;
while (<$image>) {
    $string .= $_;
}
close $image;

my @array = split //, $string;
my %bigram;
for my $i (2..$#array) {
	$bigram{$array[$i-1]}->{$array[$i]}++;
}

my @newarray = ($array[0]);
for my $i (1..$#array) {
	unless ($i % $param) {
		my $list = $bigram{$array[$i-1]};
		my @sorted = sort {$list->{$b} <=> $list->{$a}} keys %$list;
		my $estimated = $sorted[0] || $array[$i];
		
		push @newarray, $estimated;
	}
	else {
		push @newarray, $array[$i];
	}
}

print join '', @newarray;