473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jul 17 '05 #1
8 2037
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" <so*****@erols.com> wrote in message
news:1c*************************@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

Jul 17 '05 #2
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:<_r*******************@newssvr27.news.prodigy .com>...
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" <so*****@erols.com> wrote in message
news:1c*************************@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

Jul 17 '05 #3
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" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
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:<_r*******************@newssvr27.news.prodigy .com>...
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" <so*****@erols.com> wrote in message
news:1c*************************@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

Jul 17 '05 #4
"CountScubula" <me@scantek.hotmail.com> wrote in message news:<bE******************@newssvr27.news.prodigy. com>...
sorry, I didnt really look at the code, how is it breaking, error, not
working?

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

Phil

--
Mike Bradley
http://www.gzentools.com -- free online php tools
"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
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>");
** 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
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:<_r*******************@newssvr27.news.prodigy .com>... 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" <so*****@erols.com> wrote in message
news:1c*************************@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

Jul 17 '05 #5
"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@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 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
Jul 17 '05 #6
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:<vi*******************@newssvr27.news.prodigy .com>...
"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@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 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();

?>

Jul 17 '05 #7
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" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
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:<vi*******************@newssvr27.news.prodigy .com>...
"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@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 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();

?>

Jul 17 '05 #8
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:<VP*****************@newssvr29.news.prodigy.c om>...
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" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
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:<vi*******************@newssvr27.news.prodigy .com>...
"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@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 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();

?>

Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: mike kreiner | last post by:
I'm using the convolve2d(image, mask) function from scipy to blur an image. The image I'm using is 512 by 384. When I display the image that convolve2d returns, the the left-most square of pixels...
2
by: Nick Calladine | last post by:
Is this possible to ... I wish to get the value of a dropdown select but gets is indexable value (dont know if that is the right term) if that is possible (the position it assigned get assigned...
2
by: Andreas Viklund via DotNetMonster.com | last post by:
Hi! I am developing an application in ASP.NET that takes an image, that have been created with a digital camera or camera phone, and processes it, to get data from it. The image taken by the user...
1
by: Jonas | last post by:
Hi! When i use the code below to convert a wmf to a jpg image, the output will always be with a black backgound. ("data" is a byte array containing the WMF file) System.IO.MemoryStream ms =...
16
by: Nicky | last post by:
I am using Visual C# 2005 and I need help creating a filter that will turn a picture black and white. This code I have so far is: byte red, green, blue, avg, newColor; int x; int y; ...
0
by: CG3000 | last post by:
I create a .PNG image ( in Macromedia Fireworks ) which has an gif in it in the top left corner and a lot of empty canvas space to the right. I use about 10 text boxes on a form to populate that...
4
by: MrNobody | last post by:
Let's say I have an image which is monochrome with areas of white, black and shades of gray. Now I want to turn all the white parts into some RGB color but it also needs to be darkened by the gray...
1
by: alokw | last post by:
Hi everyone, Here's my problem. I'd like to revamp my web site, and I have this idea. I want to create essentially a border around the screen of about 100 pixels of just black. Heres where...
49
by: comp.lang.php | last post by:
/** * Class for grayscaling image * * @author Phil Powell * @version 1.2.1 * @package IMAGE_CATALOG::IMAGE */ class ImageGrayscaleGenerator extends ImageResizeComponents { /
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.