How to create Starbursts

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
tdan

How to create Starbursts

Post by tdan »

Does anyone have a script or any ideas for how to create a starburst?

Given: Number of points, max width of starburst, max height of starburst, length of each point on the starburst.
tdan

Re: How to create Starbursts

Post by tdan »

I make a small contribution back to the community:
beautiful starbursts in PerlMagick.

I took the code from a much larger script so it's not absolutely the best way to do things.
But it works.
The only thing that doesn't work well is the look of the image when it is rotated.
It works fine on the command line, but for some reason, PerlMagick still tends to make the image fuzzy for me when it is rotated. Anyone know why?

Anthony - this may make a nice addition to your help pages in the SVG section.

Code: Select all

#!/usr/bin/perl

use Image::Magick;
use strict;
use constant PI => 4 * atan2(1, 1);

my $image = new Image::Magick;

my @ImageData = 
{
	Type			=> 'Starburst', 
	ID				=> 'aaa9b7n35q2857bnd57q23579b2q689ba',
	ImageWidth		=> '250',
	ImageHeight	=> '150',
	Rotate			=> '-5',
	BorderWidth		=> '2',
	BorderColor		=> 'black',
	BackgroundColor	=> 'red',
	PointLength		=> '40',
	NumberOfPoints	=> '10'
};

my $count = 0;
my %xProps = %{$ImageData[$count]};

# Declare Starburst variables
my(@points, $OuterRadius, $InnerRadius, $CanvasSideLength, $x);
# Get angle between points

my $IncrementAngle = 2*PI / ( 2 * $xProps{'NumberOfPoints'} );

# Get largest Image dimension and use as starburst radius.  Will scale down this dimension at the end.
if ($xProps{'ImageWidth'} > $xProps{'ImageHeight'})
{
$CanvasSideLength = $xProps{'ImageWidth'};
$OuterRadius = $CanvasSideLength / 2;
$InnerRadius = $OuterRadius - $xProps{'PointLength'};
$x = $image->Set( size => $CanvasSideLength . 'x' . $CanvasSideLength );	warn "$x" if "$x";
}
else
{
$CanvasSideLength = $xProps{'ImageWidth'};
$OuterRadius = $CanvasSideLength / 2;
$InnerRadius = $OuterRadius - $xProps{'PointLength'};
$x = $image->Set( size => $CanvasSideLength . 'x' . $CanvasSideLength );	warn "$x" if "$x";
}

# Canvas is transparent
$x = $image->ReadImage( 'xc:none' );	warn "$x" if "$x";

my $sPoints = "M ";

# Calculate Starburst points
for ( my $CurrentAngle=0; $CurrentAngle<2*PI; $CurrentAngle += $IncrementAngle )
{
my $tmpX = $OuterRadius * cos($CurrentAngle)  +  $CanvasSideLength/2;
my $tmpY = $OuterRadius * sin($CurrentAngle)  +  $CanvasSideLength/2;
$sPoints .= "$tmpX,$tmpY ";

$CurrentAngle += $IncrementAngle;

$tmpX = $InnerRadius * cos($CurrentAngle)  +  $CanvasSideLength/2;
$tmpY = $InnerRadius * sin($CurrentAngle)  +  $CanvasSideLength/2;
$sPoints .= "$tmpX,$tmpY ";
}
# Draw line back to start (for some reason 'Z' doesn't work...)
my $tmpX = $OuterRadius * cos(0)  +  $CanvasSideLength/2;
my $tmpY = $OuterRadius * sin(0)  +  $CanvasSideLength/2;
$sPoints .= "$tmpX,$tmpY ";

$x = $image->Draw( stroke=>$xProps{'BorderColor'}, fill=>$xProps{'BackgroundColor'}, strokewidth=>$xProps{'BorderWidth'}, primitive=>'path', points=>$sPoints );	warn "$x" if "$x";

# Resize to given user dimensions
if ( $xProps{'ImageWidth'} != $xProps{'ImageHeight'} )
{
$x = $image->Resize( geometry=> $xProps{'ImageWidth'} . 'x' . $xProps{'ImageHeight'} .'!' );	warn "$x" if "$x";
}

# Rotate
if ( $xProps{'Rotate'} != 0 )
{
# color sets background color
$x = $image->Rotate( degrees=>$xProps{'Rotate'}, color=>'none');	warn "$x" if "$x";
}

# Generate output filename
$x = $image->Write( "$xProps{'ID'}.png" );	warn "$x" if "$x";
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to create Starbursts

Post by anthony »

thanks for the script. It will take me a bit of time to look though it and figure out what it does.

In the mean time I have a star bust technique that generates it from a 'randomised' image....
http://www.imagemagick.org/Usage/advanced/#stars

It isn't very nice but basically generates the stars based on a 'density' rather than a specific number of stars. also the stars are of variable bightness.

I was hoping to extend the techniue to locate the center of the brightest spots on an image, and generate a startburst at that point. all automatically.

Unfortunateally I have not succeeded in locating the center of bright spots, as I got mislead into a blind development alley.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tdan

Re: How to create Starbursts

Post by tdan »

Anthony - very cool starfield!
You could easily make the 6-pointed stars look like snowflakes by changing colors in the image.

As far as my starburst script, I'd be happy to comment it or do whatever to help you understand it.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to create Starbursts

Post by anthony »

Color it in what way.

A few short comments are always a good idea in any script. You come back six months later and ask... Why did I do that? and that is just yourself.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to create Starbursts

Post by anthony »

I took an initial look and it looks like you just draw a set of radial lines from a single point. Nice initial starting point.

It is hard to pick out the loops and conditionals untill I added appropriate indents.

Unfortantateally at this point I can't use PerlMagic on my system (some sort of weird symbol failure). so I can't just run it to see what is.

The 'Z' flag should work. But without seeing the data the script generates for the draw (by running the script and printing the result) I can't tell why it is failing.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply