Skip to content

Instantly share code, notes, and snippets.

@michel47
Last active April 11, 2021 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michel47/387528ebe6b4b7a8218aa525b572f22b to your computer and use it in GitHub Desktop.
Save michel47/387528ebe6b4b7a8218aa525b572f22b to your computer and use it in GitHub Desktop.
embed anything into a png

An image to make images ...

perl script

This little perl script makes an image out of anything ...

note: you'd need to have the program convert from imageMagick

download command:

curl -s https://i.postimg.cc/G2Ypy6sF/dat2png.png | convert png:- ppm:- | tail +4 > dat2png.pl

test and example:

echo "Why, Hello" > data.txt
curl -s https://i.postimg.cc/G2Ypy6sF/dat2png.png | convert png:- ppm:- | tail +4 | perl /dev/stdin  data.txt data.png
convert data.png ppm:- | tail +4

or

curl -s https://i.postimg.cc/G2Ypy6sF/dat2png.png | convert png:- ppm:- | tail +4 > dat2png.pl
echo "Hello world" | perl dat2png.pl | convert png:- ppm:- | tail +4

The images are gracefully hosted by postImages

#
perl dat2png.pl -o dat2png.png data2png.pl
perl dat2png.pl -o README.png README.md
perl dat2png.pl -o build.png build.sh
perl dat2png.pl -o test.png test.sh
#!/usr/bin/perl
# this script take any data and make an image with it (format .ppm)
# $Previous: QmPYNQMhMbMjQed32su9q5eNCGurGozhbiSSrh6oGf2Dva$
# deps
# convert from ImageMagick
our $dbug=0;
#--------------------------------
# -- Options parsing ...
#
my $if = undef;
my $of = undef;
while (@ARGV && $ARGV[0] =~ m/^-/)
{
$_ = shift;
#/^-(l|r|i|s)(\d+)/ && (eval "\$$1 = \$2", next);
if (/^-v(?:erbose)?/) { $verbose= 1; }
elsif (/^-?if?=?([\w.]+)?/) { $if= $1?$1:shift; }
elsif (/^-?of?=?([\w.]+)?/) { $of= $1?$1:shift; }
else { die "Unrecognized switch: $_\n"; }
}
#understand variable=value on the command line...
eval "\$$1='$2'"while $ARGV[0] =~ /^(\w+)=(.*)/ && shift;
my $data;
if (! defined $of) {
if (@ARGV) { $of = pop @ARGV }
else { $of = '-' }
}
if (defined $if) {
local *IN;
local $/ = undef;
open IN,'<',$if;
$data = <IN>;
close IN;
} else {
if ($ARGV > 0) { $if = '<>'; }
elsif ($#ARGV == 0) { $if = $ARGV[0]; }
else { $if = '-'; }
local $/ = undef;
$data = <>;
close STDIN;
}
my $size = length($data);
my $pi = atan2(0,-1);
#my $iratio = 4/3; # y/x
my $iratio = $pi; # x/y
my $xy = $size/3 * ($iratio);
my $x = sqrt($xy);
if ($verbose) {
printf STDERR "if: %s\n",$if;
printf STDERR "of: %s\n",$of;
printf STDERR "size: %s\n",$size;
printf STDERR "x: %.3f\n",$x;
printf STDERR "y: %.3f\n",$x / $iratio;
}
my $y = int($x / $iratio + $iratio);
$x = int( ( $size / 3 + $y - 1) / $y );
my $n = $x*$y*3;
my $delta = $n - $size;
if ($delta < 0) {
$x++;
$n = $x*$y*3;
$delta = $n - $size;
}
my $pad = "\x00" x $delta;
if ($verbose) {
printf STDERR "playload: %sx%s = %s\n",$x,$y,$n;
printf STDERR "delta: %s\n", $delta;
}
my $hdr = <<"EOS";
P6
$x $y
255
EOS
#my $fname = $file; $fname =~ s,.*/,,;
#my $bname = $fname; $bname =~ s/\.[^\.]*$//;
#printf STDERR "fname: %s\n",$fname;
#printf STDERR "fname: %s.png\n",$bname;
local *PPM; open PPM,"| convert -compress LZW -strip -quality 90 ppm:- png:$of";
print PPM $hdr;
binmode(PPM);
print PPM $data;
print PPM $pad;
close PPM;
exit $?;
1; # $Source: /my/perl/scripts/dat2png.pl$
echo "Why, Hello" > data.txt
curl -s https://i.postimg.cc/mDBJSLNw/dat2png.png | convert png:- ppm:- | tail +4 | perl /dev/stdin data.txt data.png
convert data.png ppm:- | tail +4
rm data.txt data.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment