Page 1 of 1

How to use ImageMagick to create randomly generated patterns?

Posted: 2019-01-27T02:23:18-07:00
by crotanite
I'm trying to use the PHP class Imagick to generate random patterns of various designs which I can then color.
I have two functions that generate random patterns of different types, but am struggling to color both the black and white sections different colors.

Code: Select all

<?php
function generate_first_pattern($base_color, $pattern_color)
{
	// create the base
    $base = new \Imagick();
    $base->newPseudoImage(500, 500, 'plasma:fractal');

    // create the pattern on the base
  	$base->thresholdImage(0.5 * \Imagick::getQuantum());
  	$base->blurImage(0, 1);

  	// clone
  	$pattern = clone $base;

  	// base
  	$base->setImageBackgroundColor($base_color);
  	$base->setImageAlphaChannel(\Imagick::ALPHACHANNEL_SHAPE);

  	// pattern
  	$pattern->setImageBackgroundColor($pattern_color);
  	$pattern->setImageAlphaChannel(\Imagick::ALPHACHANNEL_SHAPE);

    // layer
    $base->compositeImage($pattern, \Imagick::COMPOSITE_DEFAULT, 0, 0);

    // set format to png
    $pattern->setImageFormat('png');
    header('Content-Type: image/png');
    echo $pattern->getImageBlob();
}

function generate_second_pattern($base_color, $pattern_color)
{
	// create the base
	$base = new \Imagick();
	$base->newImage(500, 500, new \ImagickPixel('white'));
	$base->setImageVirtualPixelMethod(\Imagick::VIRTUALPIXELMETHOD_TILE);

	// first steps
	$base->addNoiseImage(\Imagick::NOISE_RANDOM);
  	$base->blurImage(0, 25);
    $base->normalizeImage();
    $base->separateImageChannel(\Imagick::CHANNEL_GRAY);
    $base->posterizeImage(3, \Imagick::DITHERMETHOD_FLOYDSTEINBERG);
  	$base->thresholdImage(0.5 * \Imagick::getQuantum());

  	// clone
  	$pattern = clone $base;

  	// base
    $base->setImageBackgroundColor($base_color);
    $base->setImageAlphaChannel(\Imagick::ALPHACHANNEL_SHAPE);

    // pattern
    $pattern->setImageBackgroundColor($pattern_color);
    $pattern->setImageAlphaChannel(\Imagick::ALPHACHANNEL_SHAPE);

    // layer
    $base->compositeImage($pattern, \Imagick::COMPOSITE_DEFAULT, 0, 0);

    // set format to png
    $base->setImageFormat('png');
    header('Content-Type: image/png');
    echo $base->getImageBlob();
}

//generate_first_pattern('#2ecc71', '#cc4444');
generate_second_pattern('#2ecc71', '#cc4444');
Q1. How can I color both the black and white sections different colors?
Q2. Are there any other ways to generate random patterns using ImageMagick?

Re: How to use ImageMagick to create randomly generated patterns?

Posted: 2019-01-27T08:12:46-07:00
by snibgo
For those of us without PHP, it would help if you showed sample outputs.
crotanite wrote:Q1. How can I color both the black and white sections different colors?
If you image is grayscale, "+level-colors colour1,colour2" will change black to colour1, white to colour2, and grays to intermediate colours.

Re: How to use ImageMagick to create randomly generated patterns?

Posted: 2019-01-27T12:22:54-07:00
by fmw42
You might want to look at my bash unix shell script, randomblob, at my link below. Perhaps that is what you desire. If so, you can run it from PHP exec().