jpeg image decompiling

jpeg image decompiling

am 30.08.2007 15:20:12 von William Gill

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.

Re: jpeg image decompiling

am 30.08.2007 15:44:15 von luiheidsgoeroe

On Thu, 30 Aug 2007 15:20:12 +0200, William Gill d> =

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 color=
s. =

> Several of them use PHP to accomplish this. I have requested the sour=
ce =

> 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.



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

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

-- =

Rik Wasmus

My new ISP's newsserver sucks. Anyone recommend a good one? Paying for =

quality is certainly an option.

Re: jpeg image decompiling

am 30.08.2007 16:25:00 von William Gill

>
>
>
> $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.

Re: jpeg image decompiling

am 30.08.2007 17:59:18 von William Gill

William Gill wrote:
>>
>>
>
> 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.

Re: jpeg image decompiling

am 30.08.2007 19:39:56 von luiheidsgoeroe

On Thu, 30 Aug 2007 17:59:18 +0200, William Gill d> =

wrote:

>
>
> William Gill wrote:
>>>
>>>
>> 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 colo=
r =

>> 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 >=3D 2.0, imagecolorat() should re=
turn =

> 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.

Re: jpeg image decompiling

am 30.08.2007 20:04:21 von William Gill

> 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!

Re: jpeg image decompiling

am 31.08.2007 05:01:41 von Norman Peelman

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