<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Marilyn Burgess &#187; ImageMagick</title>
	<atom:link href="http://marilynburgess.com/tag/imagemagick/feed/" rel="self" type="application/rss+xml" />
	<link>http://marilynburgess.com</link>
	<description></description>
	<lastBuildDate>Mon, 29 Aug 2011 16:33:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Changing Pixel Colors With ImageMagick</title>
		<link>http://marilynburgess.com/2009/05/changing-pixel-colors-with-imagemagick/</link>
		<comments>http://marilynburgess.com/2009/05/changing-pixel-colors-with-imagemagick/#comments</comments>
		<pubDate>Fri, 08 May 2009 21:48:49 +0000</pubDate>
		<dc:creator>marilyn</dc:creator>
				<category><![CDATA[Perl/mod_perl]]></category>
		<category><![CDATA[ImageMagick]]></category>
		<category><![CDATA[PerlMagick]]></category>

		<guid isPermaLink="false">http://blog.listcentral.me/?p=109</guid>
		<description><![CDATA[On List Central, users will be able to choose from a selection of themes for their main page. The themes available only vary by color. To make this happen I have several sets of CSS files; when the user logs in, the system checks which theme s/he selected, and serves the appropriate CSS files. These [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_267" class="wp-caption alignleft" style="width: 124px"><img class="size-full wp-image-267" title="logo" src="http://marilynburgess.com/images/2009/04/logo.jpg" alt="ImageMagick" width="114" height="118" /><p class="wp-caption-text">ImageMagick</p></div>
<p>On List Central, users will be able to choose from a selection of themes for their main page. The themes available only vary by color. To make this happen I have several sets of CSS files; when the user logs in, the system checks which theme s/he selected, and serves the appropriate CSS files. These sets of CSS files are automatically created from a base set of CSS files when a them is created in the List Central Administration.</p>
<p>Aside from the CSS files, there are several images on the users&#8217; main page that vary depending on what theme is selected. In the name of saving time in the future, I set out to figure out a way to create these images automatically as well. I really don&#8217;t like busy work, and I&#8217;m willing to go to great lengths to find a way around having to do any!</p>
<p>My general approach was, for each image, go through each and every pixel in the image, and see if it is one of the colors I want to change. If it is, then I change it, else, I don&#8217;t do anything with it. I&#8217;m assuming that the image will only have the colors I want to change in it. The code can easily be modified to handle this case differently. </p>
<p>On with the code!</p>
<div class="code">
my @BaseColors = (&#8216;#ffffff&#8217;, &#8216;#0000ff&#8217;, &#8216;#00ff00&#8242;, &#8216;#ff0000&#8242;, &#8216;#000000&#8242;);<br />
my @NewColors = (&#8216;#ffffff&#8217;, &#8216;#121d52&#8242;, &#8216;#1d687a&#8217;, &#8216;#bdeaff&#8217;, &#8216;#f8f8ff&#8217;);</p>
<p>my $imSrcImg = new Image::Magick;<br />
my $imagefile = &#8216;source_image.png&#8217;;<br />
my $warn = $imSrcImg->Read($imagefile);<br />
print &#8216;READ WARN: $warn&#8217;;</p>
<p>my ($width, $height) = $imSrcImg->Get(&#8216;width&#8217;, &#8216;height&#8217;);<br />
print &#8216;GET WARN: $warn&#8217;;</p>
<p>my $size = $width . &#8216;x&#8217; . $height;<br />
my $iMagickNew = new Image::Magick(size=>$size);<br />
$warn = $iMagickNew->ReadImage(&#8216;NULL:white&#8217;);<br />
print &#8216;ReadImage WARN: $warn&#8217;);</p>
<p># Iterate over every pixel in the image and change<br />
for my $y (0..($height-1)){<br />
&nbsp;&nbsp;&nbsp;for my $x (0..($width-1)){</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my (@pixel) = split(/,/, $imSrcImg->Get(&#8216;pixel[$x,$y]&#8216;));</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $red = DecToHex($pixel[0]);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $green = DecToHex($pixel[1]);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $blue = DecToHex($pixel[2]);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $color = &#8216;#&#8217; . $red . $green . $blue;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Check all of the colors we are going to be changing<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(my $i = 0; $i <  @BaseColors; $i++ ) {</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Change pixel color if the pixel is the color of one<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#   of our base colors but not if it's the color we<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#   want to change it to already to save a fraction<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#   of a moment<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($color eq  $BaseColors[$i] &#038;&#038; $color ne $NewColors[$i]){</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Pull out the red, green, &#038; blue components<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $r; my $g; my $b;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $changeToColor = $NewColors[$i];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($changeToColor =~ m/#([wd]{2})([wd]{2})([wd]{2})/){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$r = $1; $g = $2; $b = $3;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Convert to hex<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $red = HexToDec($r);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $green = HexToDec($g);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $blue = HexToDec($b);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $toColorHex = '#'.$r.$g.$b;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $toColorDec = '$red, $green, $blue';</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $x2 = $x + 1; my $y2 = $y + 1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my $warn = $iMagickNew->Draw(fill=>$toColorHex, primitive=>&#8217;point&#8217;, points=>&#8217;$x,$y&#8217;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &#8216;Changing pixl [$x,$y] from $color to $toColorHex ($toColorDec)nWARN: $warn&#8217;;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</p>
<p># Write the new image, then we are done<br />
my $newImage = &#8216;new_image.png&#8217;;<br />
my $warn = $iMagickNew->Write($newImage);<br />
print &#8216;WARN: $warn&#8217;;</p>
<p># Converts a decimal number to a hexidecimal number<br />
sub DecToHex {<br />
&nbsp;&nbsp;&nbsp;my $dec = shift;</p>
<p>&nbsp;&nbsp;&nbsp;my $hex = sprintf(&#8216;%X&#8217;, $dec);<br />
&nbsp;&nbsp;&nbsp;if($hex =~ m/^([wd]{2})/){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$hex = $1;<br />
&nbsp;&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;&nbsp;$hex = lc($hex);<br />
&nbsp;&nbsp;&nbsp;return $hex;<br />
} </p>
<p># Converts a hexidecimal number to a decimal number<br />
sub HexToDec {<br />
&nbsp;&nbsp;&nbsp;my $hex = shift;</p>
<p>&nbsp;&nbsp;&nbsp;my $dec = hex($hex);</p>
<p>&nbsp;&nbsp;&nbsp;return $dec;<br />
}
</p></div>
<p><strong>WARNING</strong>: This code will not work as is. There are apostrophes that should be double apostrophes and other such aesthetic changes. </p>
<p>I&#8217;ve played around with ImageMagick in the past, so it was the logical choice for my image manipulation package.</p>
<p>I had one major sticking point in trying to get my pixels to change color, which all stemmed from the lack of quality documentation available for the ImageMagick perl package. I love PerlMagick! It is super powerful! It has always baffled me how poor the documentation is. Hopefully my sharing this tidbit will help some other poor soul out there who is struggling with Perl Magick. </p>
<p>Here&#8217;s the crux of it: You cannot get a pixel to display in a certain color with:</p>
<div class="code">
$iMagick->Set(&#8216;pixel[49,49]&#8216;=>&#8217;red&#8217;);
</div>
<p>as you might assume from the documentation. You have to use the &#8216;Draw&#8217; subroutine like this:</p>
<div class="code">
$iMagick->Draw(fill=>red, primitive=>&#8217;point&#8217;, points=>&#8217;49,49);
</div>
<p>It took me hours to figure that out! </p>
<p>I hope my future users like the whole theme selection business on List Central after all of this effort! It was fun to make it happen though! Its a win win!</p>
<h3  class="related_post_title">You might also like...</h3><ul class="related_post"><li><a href="http://marilynburgess.com/2009/07/send-email-from-perl-routed-through-google-apps-smtp/" title="Send email from Perl routed through Google Apps SMTP ">Send email from Perl routed through Google Apps SMTP </a></li><li><a href="http://marilynburgess.com/2009/06/a-brief-history-of-perl/" title="A Brief History of Perl">A Brief History of Perl</a></li><li><a href="http://marilynburgess.com/2009/05/companies-that-use-perl/" title="Companies that Use Perl">Companies that Use Perl</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://marilynburgess.com/2009/05/changing-pixel-colors-with-imagemagick/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

