Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 03:55 AM
Phil Powell
Guest
 
Posts: n/a
Default Grayscaling Image with GD causes image to turn jet black!

I borrowed this code from a source:



for($a=0;$a<imagecolorstotal ($image_id);$a++)
{
$color = imageColorsForIndex($image_id,$i);
$R=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
($color['blue']);
$G=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
($color['blue']);
$B=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
($color['blue']);
imageColorSet($image_id, $a, $R, $G, $B);
}

using an image (in this case I'm using a JPEG) that is a true color
image I was unable to successfully use imagecopymergegray since that
washed out my hues/saturation altogether making the image illegible
(imagecopymergegray + imagecreate); otherwise, the colors overrode
imagecopymergegray (imagecopymergegray + imagecreatetruecolor).

Using the code I borrowed I was able to change the image.. except that
it turned jet black instead!

Has anyone ever successfully grayscaled an image using GD library? If
so, how did you do it?

Thanx
Phil



  #2  
Old July 17th, 2005, 03:55 AM
CountScubula
Guest
 
Posts: n/a
Default Re: Grayscaling Image with GD causes image to turn jet black!

Try running an average of the three colors:
$avg = floor( ( $color['red']) + $color['green']) + $color['blue']) ) / 3 )
$R= $avg; $B = $avg; $G = $avg;
imageColorSet($image_id, $a, $R, $G, $B);


--
Mike Bradley
http://www.gzentools.com -- free online php tools
"Phil Powell" <soazine@erols.com> wrote in message
news:1cdca2a7.0402030910.a9109fc@posting.google.co m...[color=blue]
> I borrowed this code from a source:
>
>
>
> for($a=0;$a<imagecolorstotal ($image_id);$a++)
> {
> $color = imageColorsForIndex($image_id,$i);
> $R=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
> ($color['blue']);
> $G=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
> ($color['blue']);
> $B=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
> ($color['blue']);
> imageColorSet($image_id, $a, $R, $G, $B);
> }
>
> using an image (in this case I'm using a JPEG) that is a true color
> image I was unable to successfully use imagecopymergegray since that
> washed out my hues/saturation altogether making the image illegible
> (imagecopymergegray + imagecreate); otherwise, the colors overrode
> imagecopymergegray (imagecopymergegray + imagecreatetruecolor).
>
> Using the code I borrowed I was able to change the image.. except that
> it turned jet black instead!
>
> Has anyone ever successfully grayscaled an image using GD library? If
> so, how did you do it?
>
> Thanx
> Phil[/color]


  #3  
Old July 17th, 2005, 03:56 AM
Phil Powell
Guest
 
Posts: n/a
Default Re: Grayscaling Image with GD causes image to turn jet black!

I'm sorry but that too breaks. Perhaps a code review is in order, I
realize that imageColorsTotal($this->image) produces 0, which I am
afraid I don't understand:

class ImageGrayscaleGenerator {

/*---------------------------------------------------------------------------------------------------------------------------------------------------------
This class exists due to the rather poor performance of the
imagecopymergegray() command
Borrowing code snippet from
http://us3.php.net/manual/en/functio...ymergegray.php first
line comment
Will perform actual grayscaling of image one pixel at a time.
-----------------------------------------------------------------------------------------------------------------------------------------------------------*/

var $image, $image_width, $image_height;

// REMEMBER TO USE REFERENCE POINTER ONTO $image OBJECT TO ENSURE
"static" CHANGE TO OBJECT AND NOT TO INSTANCE

function ImageGrayscaleGenerator(&$image) { // CONSTRUCTOR
$this->image =& $image;
}

function makeGray() { // VOID METHOD
print_r($this->image); print_r("<P>");
print_r(@imageColorsTotal($this->image) . "<P>");
for ($i = 0; $i < @imagecolorstotal($this->image); $i++) {
$colorArray = @imageColorsForIndex($this->image, $i);
print_r($colorArray); print_r("<P>");
$avg = floor(($colorArray['red'] + $colorArray['green'] +
$colorArray['blue']) / 3);
@imageColorSet($this->image, $i, $avg, $avg, $avg);
}
}
}


Thanx
Phil

"CountScubula" <me@scantek.hotmail.com> wrote in message news:<_rSTb.19724$up5.18401@newssvr27.news.prodigy .com>...[color=blue]
> Try running an average of the three colors:
> $avg = floor( ( $color['red']) + $color['green']) + $color['blue']) ) / 3 )
> $R= $avg; $B = $avg; $G = $avg;
> imageColorSet($image_id, $a, $R, $G, $B);
>
>
> --
> Mike Bradley
> http://www.gzentools.com -- free online php tools
> "Phil Powell" <soazine@erols.com> wrote in message
> news:1cdca2a7.0402030910.a9109fc@posting.google.co m...[color=green]
> > I borrowed this code from a source:
> >
> >
> >
> > for($a=0;$a<imagecolorstotal ($image_id);$a++)
> > {
> > $color = imageColorsForIndex($image_id,$i);
> > $R=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
> > ($color['blue']);
> > $G=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
> > ($color['blue']);
> > $B=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
> > ($color['blue']);
> > imageColorSet($image_id, $a, $R, $G, $B);
> > }
> >
> > using an image (in this case I'm using a JPEG) that is a true color
> > image I was unable to successfully use imagecopymergegray since that
> > washed out my hues/saturation altogether making the image illegible
> > (imagecopymergegray + imagecreate); otherwise, the colors overrode
> > imagecopymergegray (imagecopymergegray + imagecreatetruecolor).
> >
> > Using the code I borrowed I was able to change the image.. except that
> > it turned jet black instead!
> >
> > Has anyone ever successfully grayscaled an image using GD library? If
> > so, how did you do it?
> >
> > Thanx
> > Phil[/color][/color]
  #4  
Old July 17th, 2005, 03:57 AM
CountScubula
Guest
 
Posts: n/a
Default Re: Grayscaling Image with GD causes image to turn jet black!

sorry, I didnt really look at the code, how is it breaking, error, not
working?

--
Mike Bradley
http://www.gzentools.com -- free online php tools
"Phil Powell" <soazine@erols.com> wrote in message
news:1cdca2a7.0402040711.63478c64@posting.google.c om...[color=blue]
> I'm sorry but that too breaks. Perhaps a code review is in order, I
> realize that imageColorsTotal($this->image) produces 0, which I am
> afraid I don't understand:
>
> class ImageGrayscaleGenerator {
>
>[/color]
/*--------------------------------------------------------------------------
----------------------------------------------------------------------------
---[color=blue]
> This class exists due to the rather poor performance of the
> imagecopymergegray() command
> Borrowing code snippet from
> http://us3.php.net/manual/en/functio...ymergegray.php first
> line comment
> Will perform actual grayscaling of image one pixel at a time.
> --------------------------------------------------------------------------[/color]
----------------------------------------------------------------------------
-----*/[color=blue]
>
> var $image, $image_width, $image_height;
>
> // REMEMBER TO USE REFERENCE POINTER ONTO $image OBJECT TO ENSURE
> "static" CHANGE TO OBJECT AND NOT TO INSTANCE
>
> function ImageGrayscaleGenerator(&$image) { // CONSTRUCTOR
> $this->image =& $image;
> }
>
> function makeGray() { // VOID METHOD
> print_r($this->image); print_r("<P>");
> print_r(@imageColorsTotal($this->image) . "<P>");
> for ($i = 0; $i < @imagecolorstotal($this->image); $i++) {
> $colorArray = @imageColorsForIndex($this->image, $i);
> print_r($colorArray); print_r("<P>");
> $avg = floor(($colorArray['red'] + $colorArray['green'] +
> $colorArray['blue']) / 3);
> @imageColorSet($this->image, $i, $avg, $avg, $avg);
> }
> }
> }
>
>
> Thanx
> Phil
>
> "CountScubula" <me@scantek.hotmail.com> wrote in message[/color]
news:<_rSTb.19724$up5.18401@newssvr27.news.prodigy .com>...[color=blue][color=green]
> > Try running an average of the three colors:
> > $avg = floor( ( $color['red']) + $color['green']) + $color['blue']) ) /[/color][/color]
3 )[color=blue][color=green]
> > $R= $avg; $B = $avg; $G = $avg;
> > imageColorSet($image_id, $a, $R, $G, $B);
> >
> >
> > --
> > Mike Bradley
> > http://www.gzentools.com -- free online php tools
> > "Phil Powell" <soazine@erols.com> wrote in message
> > news:1cdca2a7.0402030910.a9109fc@posting.google.co m...[color=darkred]
> > > I borrowed this code from a source:
> > >
> > >
> > >
> > > for($a=0;$a<imagecolorstotal ($image_id);$a++)
> > > {
> > > $color = imageColorsForIndex($image_id,$i);
> > > $R=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
> > > ($color['blue']);
> > > $G=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
> > > ($color['blue']);
> > > $B=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
> > > ($color['blue']);
> > > imageColorSet($image_id, $a, $R, $G, $B);
> > > }
> > >
> > > using an image (in this case I'm using a JPEG) that is a true color
> > > image I was unable to successfully use imagecopymergegray since that
> > > washed out my hues/saturation altogether making the image illegible
> > > (imagecopymergegray + imagecreate); otherwise, the colors overrode
> > > imagecopymergegray (imagecopymergegray + imagecreatetruecolor).
> > >
> > > Using the code I borrowed I was able to change the image.. except that
> > > it turned jet black instead!
> > >
> > > Has anyone ever successfully grayscaled an image using GD library? If
> > > so, how did you do it?
> > >
> > > Thanx
> > > Phil[/color][/color][/color]


  #5  
Old July 17th, 2005, 03:57 AM
Phil Powell
Guest
 
Posts: n/a
Default Re: Grayscaling Image with GD causes image to turn jet black!

"CountScubula" <me@scantek.hotmail.com> wrote in message news:<bEdUb.20202$CX7.1043@newssvr27.news.prodigy. com>...[color=blue]
> sorry, I didnt really look at the code, how is it breaking, error, not
> working?[/color]


I'll show you, just go down and view...

Phil
[color=blue]
>
> --
> Mike Bradley
> http://www.gzentools.com -- free online php tools
> "Phil Powell" <soazine@erols.com> wrote in message
> news:1cdca2a7.0402040711.63478c64@posting.google.c om...[color=green]
> > I'm sorry but that too breaks. Perhaps a code review is in order, I
> > realize that imageColorsTotal($this->image) produces 0, which I am
> > afraid I don't understand:
> >
> > class ImageGrayscaleGenerator {
> >
> >[/color]
> /*--------------------------------------------------------------------------
> ----------------------------------------------------------------------------
> ---[color=green]
> > This class exists due to the rather poor performance of the
> > imagecopymergegray() command
> > Borrowing code snippet from
> > http://us3.php.net/manual/en/functio...ymergegray.php first
> > line comment
> > Will perform actual grayscaling of image one pixel at a time.
> > --------------------------------------------------------------------------[/color]
> ----------------------------------------------------------------------------
> -----*/[color=green]
> >
> > var $image, $image_width, $image_height;
> >
> > // REMEMBER TO USE REFERENCE POINTER ONTO $image OBJECT TO ENSURE
> > "static" CHANGE TO OBJECT AND NOT TO INSTANCE
> >
> > function ImageGrayscaleGenerator(&$image) { // CONSTRUCTOR
> > $this->image =& $image;
> > }
> >
> > function makeGray() { // VOID METHOD
> > print_r($this->image); print_r("<P>");
> > print_r(@imageColorsTotal($this->image) . "<P>");[/color][/color]

** STOP HERE! What happens is that imageColorsTotal($this->image) =
0! Though the image is in color and I do a reference pointer to the
$image resource paramters in the method call, and it actually DOES
change the image according to the value of
imageColorsTotal($this->image), it turns the entire image into one
jet-black image, no tone, no hue, nothing, a big black box.

Phil
[color=blue][color=green]
> > for ($i = 0; $i < @imagecolorstotal($this->image); $i++) {
> > $colorArray = @imageColorsForIndex($this->image, $i);
> > print_r($colorArray); print_r("<P>");
> > $avg = floor(($colorArray['red'] + $colorArray['green'] +
> > $colorArray['blue']) / 3);
> > @imageColorSet($this->image, $i, $avg, $avg, $avg);
> > }
> > }
> > }
> >
> >
> > Thanx
> > Phil
> >
> > "CountScubula" <me@scantek.hotmail.com> wrote in message[/color]
> news:<_rSTb.19724$up5.18401@newssvr27.news.prodigy .com>...[color=green][color=darkred]
> > > Try running an average of the three colors:
> > > $avg = floor( ( $color['red']) + $color['green']) + $color['blue']) ) /[/color][/color]
> 3 )[color=green][color=darkred]
> > > $R= $avg; $B = $avg; $G = $avg;
> > > imageColorSet($image_id, $a, $R, $G, $B);
> > >
> > >
> > > --
> > > Mike Bradley
> > > http://www.gzentools.com -- free online php tools
> > > "Phil Powell" <soazine@erols.com> wrote in message
> > > news:1cdca2a7.0402030910.a9109fc@posting.google.co m...
> > > > I borrowed this code from a source:
> > > >
> > > >
> > > >
> > > > for($a=0;$a<imagecolorstotal ($image_id);$a++)
> > > > {
> > > > $color = imageColorsForIndex($image_id,$i);
> > > > $R=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
> > > > ($color['blue']);
> > > > $G=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
> > > > ($color['blue']);
> > > > $B=.299 * ($color['red'])+ .587 * ($color['green'])+ .114 *
> > > > ($color['blue']);
> > > > imageColorSet($image_id, $a, $R, $G, $B);
> > > > }
> > > >
> > > > using an image (in this case I'm using a JPEG) that is a true color
> > > > image I was unable to successfully use imagecopymergegray since that
> > > > washed out my hues/saturation altogether making the image illegible
> > > > (imagecopymergegray + imagecreate); otherwise, the colors overrode
> > > > imagecopymergegray (imagecopymergegray + imagecreatetruecolor).
> > > >
> > > > Using the code I borrowed I was able to change the image.. except that
> > > > it turned jet black instead!
> > > >
> > > > Has anyone ever successfully grayscaled an image using GD library? If
> > > > so, how did you do it?
> > > >
> > > > Thanx
> > > > Phil[/color][/color][/color]
  #6  
Old July 17th, 2005, 03:57 AM
CountScubula
Guest
 
Posts: n/a
Default Re: Grayscaling Image with GD causes image to turn jet black!

"Phil Powell" <soazine@erols.com> wrote in message
news:1cdca2a7.0402042019.1ed5fcce@posting.google.c om...[color=blue]
>
> I'll show you, just go down and view...
>
> Phil
>
> < Big-O-snip >[/color]

Sometimes easier to re-invent the wheel (thats my my cars don't have model-t
wheels on them)
here is some code that works, and you can test here:
http://www.gzentools.com/test/img2gry.php



<?php

$imgFile = "girl1.jpg";

$imSrc = imagecreatefromjpeg($imgFile);

$imW = imagesx($imSrc);
$imH = imagesy($imSrc);

$imDst = imagecreate($imW,$imH);

// create 256 shades of grey
for ($i=0; $i<256; $i++) {
$colorNdx[$i] = imagecolorallocate($imDst,$i,$i,$i);
}

for ($y=0; $y<$imH; $y++)
{
for ($x=0; $x<$imW; $x++)
{
$ndx = imagecolorat($imSrc,$x,$y);
$ndxColors = imagecolorsforindex($imSrc,$ndx);
$avg = floor( ($ndxColors['red'] + $ndxColors['green'] +
$ndxColors['blue']) / 3 );
imagesetpixel($imDst,$x,$y,$colorNdx[$avg]);
}
}

imagejpeg($imDst);
exit();

?>


--
Mike Bradley
http://www.gzentools.com -- free online php tools


  #7  
Old July 17th, 2005, 03:58 AM
Phil Powell
Guest
 
Posts: n/a
Default Re: Grayscaling Image with GD causes image to turn jet black!

IT WORKED AT LAST!!! Thanx a lot. By the way, your URL produced
ASCII-related garbage on my screen, just so you know.

Thanx again!!
Phil

"CountScubula" <me@scantek.hotmail.com> wrote in message news:<vikUb.20369$ja5.12921@newssvr27.news.prodigy .com>...[color=blue]
> "Phil Powell" <soazine@erols.com> wrote in message
> news:1cdca2a7.0402042019.1ed5fcce@posting.google.c om...[color=green]
> >
> > I'll show you, just go down and view...
> >
> > Phil
> >
> > < Big-O-snip >[/color]
>
> Sometimes easier to re-invent the wheel (thats my my cars don't have model-t
> wheels on them)
> here is some code that works, and you can test here:
> http://www.gzentools.com/test/img2gry.php
>
>
>
> <?php
>
> $imgFile = "girl1.jpg";
>
> $imSrc = imagecreatefromjpeg($imgFile);
>
> $imW = imagesx($imSrc);
> $imH = imagesy($imSrc);
>
> $imDst = imagecreate($imW,$imH);
>
> // create 256 shades of grey
> for ($i=0; $i<256; $i++) {
> $colorNdx[$i] = imagecolorallocate($imDst,$i,$i,$i);
> }
>
> for ($y=0; $y<$imH; $y++)
> {
> for ($x=0; $x<$imW; $x++)
> {
> $ndx = imagecolorat($imSrc,$x,$y);
> $ndxColors = imagecolorsforindex($imSrc,$ndx);
> $avg = floor( ($ndxColors['red'] + $ndxColors['green'] +
> $ndxColors['blue']) / 3 );
> imagesetpixel($imDst,$x,$y,$colorNdx[$avg]);
> }
> }
>
> imagejpeg($imDst);
> exit();
>
> ?>[/color]
  #8  
Old July 17th, 2005, 03:58 AM
CountScubula
Guest
 
Posts: n/a
Default Re: Grayscaling Image with GD causes image to turn jet black!

Like like I forgot to inflate the tire after I reinvented the wheel, forgot
to put:
header("Content-type: image/jpeg");

--
Mike Bradley
http://www.gzentools.com -- free online php tools
"Phil Powell" <soazine@erols.com> wrote in message
news:1cdca2a7.0402051025.2dc1805b@posting.google.c om...[color=blue]
> IT WORKED AT LAST!!! Thanx a lot. By the way, your URL produced
> ASCII-related garbage on my screen, just so you know.
>
> Thanx again!!
> Phil
>
> "CountScubula" <me@scantek.hotmail.com> wrote in message[/color]
news:<vikUb.20369$ja5.12921@newssvr27.news.prodigy .com>...[color=blue][color=green]
> > "Phil Powell" <soazine@erols.com> wrote in message
> > news:1cdca2a7.0402042019.1ed5fcce@posting.google.c om...[color=darkred]
> > >
> > > I'll show you, just go down and view...
> > >
> > > Phil
> > >
> > > < Big-O-snip >[/color]
> >
> > Sometimes easier to re-invent the wheel (thats my my cars don't have[/color][/color]
model-t[color=blue][color=green]
> > wheels on them)
> > here is some code that works, and you can test here:
> > http://www.gzentools.com/test/img2gry.php
> >
> >
> >
> > <?php
> >
> > $imgFile = "girl1.jpg";
> >
> > $imSrc = imagecreatefromjpeg($imgFile);
> >
> > $imW = imagesx($imSrc);
> > $imH = imagesy($imSrc);
> >
> > $imDst = imagecreate($imW,$imH);
> >
> > // create 256 shades of grey
> > for ($i=0; $i<256; $i++) {
> > $colorNdx[$i] = imagecolorallocate($imDst,$i,$i,$i);
> > }
> >
> > for ($y=0; $y<$imH; $y++)
> > {
> > for ($x=0; $x<$imW; $x++)
> > {
> > $ndx = imagecolorat($imSrc,$x,$y);
> > $ndxColors = imagecolorsforindex($imSrc,$ndx);
> > $avg = floor( ($ndxColors['red'] + $ndxColors['green'] +
> > $ndxColors['blue']) / 3 );
> > imagesetpixel($imDst,$x,$y,$colorNdx[$avg]);
> > }
> > }
> >
> > imagejpeg($imDst);
> > exit();
> >
> > ?>[/color][/color]


  #9  
Old July 17th, 2005, 03:59 AM
Phil Powell
Guest
 
Posts: n/a
Default Re: Grayscaling Image with GD causes image to turn jet black!

Sorry I can't add that header() line, the moment I do, "index.php"
turns literally into an indiscernible JPEG image (that's assuming I'm
working with a JPEG, in my Image Catalog it could literally be
anything, even a video!)

Phil

"CountScubula" <me@scantek.hotmail.com> wrote in message news:<VPwUb.9830$G95.7608@newssvr29.news.prodigy.c om>...[color=blue]
> Like like I forgot to inflate the tire after I reinvented the wheel, forgot
> to put:
> header("Content-type: image/jpeg");
>
> --
> Mike Bradley
> http://www.gzentools.com -- free online php tools
> "Phil Powell" <soazine@erols.com> wrote in message
> news:1cdca2a7.0402051025.2dc1805b@posting.google.c om...[color=green]
> > IT WORKED AT LAST!!! Thanx a lot. By the way, your URL produced
> > ASCII-related garbage on my screen, just so you know.
> >
> > Thanx again!!
> > Phil
> >
> > "CountScubula" <me@scantek.hotmail.com> wrote in message[/color]
> news:<vikUb.20369$ja5.12921@newssvr27.news.prodigy .com>...[color=green][color=darkred]
> > > "Phil Powell" <soazine@erols.com> wrote in message
> > > news:1cdca2a7.0402042019.1ed5fcce@posting.google.c om...
> > > >
> > > > I'll show you, just go down and view...
> > > >
> > > > Phil
> > > >
> > > > < Big-O-snip >
> > >
> > > Sometimes easier to re-invent the wheel (thats my my cars don't have[/color][/color]
> model-t[color=green][color=darkred]
> > > wheels on them)
> > > here is some code that works, and you can test here:
> > > http://www.gzentools.com/test/img2gry.php
> > >
> > >
> > >
> > > <?php
> > >
> > > $imgFile = "girl1.jpg";
> > >
> > > $imSrc = imagecreatefromjpeg($imgFile);
> > >
> > > $imW = imagesx($imSrc);
> > > $imH = imagesy($imSrc);
> > >
> > > $imDst = imagecreate($imW,$imH);
> > >
> > > // create 256 shades of grey
> > > for ($i=0; $i<256; $i++) {
> > > $colorNdx[$i] = imagecolorallocate($imDst,$i,$i,$i);
> > > }
> > >
> > > for ($y=0; $y<$imH; $y++)
> > > {
> > > for ($x=0; $x<$imW; $x++)
> > > {
> > > $ndx = imagecolorat($imSrc,$x,$y);
> > > $ndxColors = imagecolorsforindex($imSrc,$ndx);
> > > $avg = floor( ($ndxColors['red'] + $ndxColors['green'] +
> > > $ndxColors['blue']) / 3 );
> > > imagesetpixel($imDst,$x,$y,$colorNdx[$avg]);
> > > }
> > > }
> > >
> > > imagejpeg($imDst);
> > > exit();
> > >
> > > ?>[/color][/color][/color]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.