By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,905 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,905 IT Pros & Developers. It's quick & easy.

jpeg image decompiling

P: n/a
I have found a couple of sites that allow a visitor to upload an image
and the site returns either the "average color", or a palette of colors.
Several of them use PHP to accomplish this. I have requested the source
code, but have not gotten a response (even though one site states
"source code available on request"). It is obvious that they read the
colors of each pixel and them manipulate that data. I would like to be
able to do something similar.

I'm not very familiar with the PHP image functions, but have been able
to figure out how to read pixel color on indexed color images, but not
on jpegs. The best I can do now is convert a jpeg to a 256 indexed
color image, and then read pixel color. Unfortunately too much of the
original data gets lost.

Does anyone know how to determine the color of jpeg pixels, or can you
point me to a good tutorial on PHP image functions.

For what it's worth, one of the things I want to do is determine the
distribution of each color (i.e. 345 pixels of rgb1, 2436 pixels of
rgb2, etc.), and possibly manipulate the image by removing and/or
replacing any given color.


Aug 30 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Thu, 30 Aug 2007 15:20:12 +0200, William Gill <no*****@example.invalid
wrote:
I have found a couple of sites that allow a visitor to upload an image
and the site returns either the "average color", or a palette of colors.
Several of them use PHP to accomplish this. I have requested the source
code, but have not gotten a response (even though one site states
"source code available on request"). It is obvious that they read the
colors of each pixel and them manipulate that data. I would like to be
able to do something similar.

I'm not very familiar with the PHP image functions, but have been able
to figure out how to read pixel color on indexed color images, but not
on jpegs. The best I can do now is convert a jpeg to a 256 indexed
color image, and then read pixel color. Unfortunately too much of the
original data gets lost.

Does anyone know how to determine the color of jpeg pixels, or can you
point me to a good tutorial on PHP image functions.
<http://nl3.php.net/imagecolorat>

$im = imagecreatefromjpeg("/your/image/file");

$pixels = array();
for($x = 0; $x < imagesx($im); $x++){
for($y = 0; $y < imagesy($im);$y++){
$rgb = imagecolorat($im, $x, $y);
//either:
$pixels[$x][$y] = imagecolorsforindex($im,$rgb);
//or:
$r = ($rgb >16) & 0xFF;
$g = ($rgb >8) & 0xFF;
$b = $rgb & 0xFF;
$pixels[$x][$y] = array($r,$g,$b);
}
}

--
Rik Wasmus

My new ISP's newsserver sucks. Anyone recommend a good one? Paying for
quality is certainly an option.
Aug 30 '07 #2

P: n/a
>
<http://nl3.php.net/imagecolorat>

$im = imagecreatefromjpeg("/your/image/file");

$pixels = array();
for($x = 0; $x < imagesx($im); $x++){
for($y = 0; $y < imagesy($im);$y++){
$rgb = imagecolorat($im, $x, $y);
//either:
$pixels[$x][$y] = imagecolorsforindex($im,$rgb);
//or:
$r = ($rgb >16) & 0xFF;
$g = ($rgb >8) & 0xFF;
$b = $rgb & 0xFF;
$pixels[$x][$y] = array($r,$g,$b);
}
}
Thanks, Rik.

I played with imagecolorat() before, but it didn't seem to work with a
non indexed color image (i.e. it worked with a gif, but not a jpeg).
Reading the spec seemed to confirm that the function returns the color
index (not the color), which can then be read for rgb values. I was
able to convert the jpeg to an indexed image, then use imagecolorat(),
but it meant reducing the original to 256 colors first.

I will try again, using your snippet, and let you know.
Aug 30 '07 #3

P: n/a


William Gill wrote:
>>
<http://nl3.php.net/imagecolorat>

I played with imagecolorat() before, but it didn't seem to work with a
non indexed color image (i.e. it worked with a gif, but not a jpeg).
Reading the spec seemed to confirm that the function returns the color
index (not the color), which can then be read for rgb values. I was
able to convert the jpeg to an indexed image, then use imagecolorat(),
but it meant reducing the original to 256 colors first.

I will try again, using your snippet, and let you know.
further digging says on PHP with GD >= 2.0, imagecolorat() should return
rgb on truecolor images. I need to do some more digging and testing.
Aug 30 '07 #4

P: n/a
On Thu, 30 Aug 2007 17:59:18 +0200, William Gill <no*****@example.invalid
wrote:
>

William Gill wrote:
>>>
<http://nl3.php.net/imagecolorat>
I played with imagecolorat() before, but it didn't seem to work witha
non indexed color image (i.e. it worked with a gif, but not a jpeg).
Reading the spec seemed to confirm that the function returns the color
index (not the color), which can then be read for rgb values. I was
able to convert the jpeg to an indexed image, then use imagecolorat(),
but it meant reducing the original to 256 colors first.
I will try again, using your snippet, and let you know.
further digging says on PHP with GD >= 2.0, imagecolorat() should return
rgb on truecolor images. I need to do some more digging and testing.
Indeed. Use print_r(gd_info()); to see the specifics of your gd version.

--
Rik Wasmus

My new ISP's newsserver sucks. Anyone recommend a good one? Paying for
quality is certainly an option.
Aug 30 '07 #5

P: n/a
Indeed. Use print_r(gd_info()); to see the specifics of your gd version.
v 2.0.34, and I am getting rgb info now. I must have done something
wrong before.

Thanks again!
Aug 30 '07 #6

P: n/a
William Gill wrote:
I have found a couple of sites that allow a visitor to upload an image
and the site returns either the "average color", or a palette of colors.
Several of them use PHP to accomplish this. I have requested the source
code, but have not gotten a response (even though one site states
"source code available on request"). It is obvious that they read the
colors of each pixel and them manipulate that data. I would like to be
able to do something similar.

I'm not very familiar with the PHP image functions, but have been able
to figure out how to read pixel color on indexed color images, but not
on jpegs. The best I can do now is convert a jpeg to a 256 indexed
color image, and then read pixel color. Unfortunately too much of the
original data gets lost.

Does anyone know how to determine the color of jpeg pixels, or can you
point me to a good tutorial on PHP image functions.

For what it's worth, one of the things I want to do is determine the
distribution of each color (i.e. 345 pixels of rgb1, 2436 pixels of
rgb2, etc.), and possibly manipulate the image by removing and/or
replacing any given color.

You really don't 'need' PHP for this... take a look at imagemagick at

http://www.imagemagick.org

and

http://www.imagemagick.org/Usage/compare/#metrics

Norm
Aug 31 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.