📜 ⬆️ ⬇️

Soothe your nerves with Perl and GD

Foreword


I am one of those people who are often subject to stressful situations. You can get rid of stress in different ways. In this topic, I will show you how I do it.

How did it come to my mind


One day while walking on the Internet, I came across one amazing image - it had a solid black background, chaotically scattered lines and points of warm-violet color. I tried to imagine what could be depicted in this image. The image did not carry any semantic load, but it was surprisingly pleasant to look at it. I imagined that this is a kind of graphic image of a space theme. My eye muscles relaxed and I felt the mysterious effect of color therapy.

After a while I decided to try to recreate something like this programmatically. Since my language was Perl, I turned to him. In my ActiveState Perl 5.14.2, the GD module was installed by default, and I decided that I would use it.
')

Operating principle


First we need a script that will create us images with chaotic overlays:
#!/usr/bin/env perl use strict; use GD; my $res = 16000; my $outres = 1000; my $format = 2.0; for my $z(0..20) { my ($c1, $c2, $c3, $c4); my $img = new GD::Image($res, $res); $img->fill($res, $res => $img->colorAllocate(0, 0, 0)); for (0..1200) { $c1 = $img->colorAllocate(rand(0xFF-0x32)+0x32, rand(0xFF-0x32)+0x32, rand(0xFF-0x32)+0x32); $c2 = $img->colorAllocate(rand(0xFF-0x32)+0x32, rand(0xFF-0x32)+0x32, rand(0xFF-0x32)+0x32); $c3 = $img->colorAllocate(rand(0xFF-0x32)+0x32, rand(0xFF-0x32)+0x32, rand(0xFF-0x32)+0x32); $c4 = $img->colorAllocate(rand(0xFF-0x32)+0x32, rand(0xFF-0x32)+0x32, rand(0xFF-0x32)+0x32); $img->setStyle($c1, $c1, $c2, $c2, $c3, $c3, $c4, $c4, gdTransparent, gdTransparent); (rand(10)>2) ? ($img->line(rand($res), rand($res), rand($res), rand($res), gdStyled)): ((rand(10)>2) ? $img->rectangle(rand($res), rand($res), rand($res), rand($res), gdStyled): $img->ellipse(rand($res), rand($res), rand($res), rand($res), gdStyled)) if (rand(10)>2); } print "processing $z.png...\n"; my $m = new GD::Image($outres*$format, $outres); $m->copyResized($img, 0, 0, 0, 0, $outres*$format, $outres, $res, $res); open F => '>'.$z.'.png'; binmode F; print F $m->png; close F; } 



Result Examples






Links


GD Perl Module
Color Therapy on Wikipedia

PS : Pictures in the example turned out 800x400, because they squeezed Habrastoreydzh. The natural scale of these images is 2000x1000

Source: https://habr.com/ru/post/132132/


All Articles