How to use ImageMagick to create randomly generated patterns?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
crotanite
Posts: 1
Joined: 2019-01-27T02:16:38-07:00
Authentication code: 1152

How to use ImageMagick to create randomly generated patterns?

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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().
Post Reply