|
Posted by Kimmo Laine on 03/22/07 08:04
"Lars Erik Bryld" <larserik@dadlnet.invalid> wrote in message
news:31ddofla5n3x.dlg@lebryld.fqdn...
> Hello group
>
> I am trying to convert a (supposedly) working python script into php.
> The script reads a colour gif-picture and produces a greyscale copy of
> it.
>
> The method is to first copy the gif header, then establish the size of
> the colour palette, then do a simple averaging of each colour byte
> triplet into a grayscale equivalent, and finally to copy the remaining
> part of the gif file unaltered over into the new file. I believe, it's
> the last procedure that goes wrong, partly because of my inability to
> understand the file manipulation issues.
>
>
> The original Python script:
>
> header = f.read(13)
> o.write(header)
> for i in range (1 << (( ord(header)[10]) & 7) + 1)):
> gray = (ord(f.read(1) + ord(f.read(1) + ord(f.read(1)))/3
> o.write(3*chr(gray))
> o.write(f.read())
>
>
> My feeble and as yet non-functional PHP translation:
>
> $fi = fopen("in.gif", 'r');
> $fo = fopen("out.gif", 'w');
>
> $header = fread($fi, 13);
> fwrite($fo, $header);
>
> $l = (1 << (ord($header[10]) & 7) + 1);
Have you tested that this actually gives the correct value? Just that it
looks a bit.. hmm.. error-bound? :) Although it does seem to be the same as
the original...
>
> for ($i = 0; $i <= $l; $i++) {
> $gray = (ord(fread($fi, 1)) + ord(fread($fi, 1)) + ord(fread($fi,
> 1)))/3;
> fwrite($fo, 3*chr($gray));
If the goal here is to write the same value three times (for r,g,b) you want
str_repeat(chr($gray),3). 3*chr($gray) will give you unexpected results.
> }
>
> $rest = fread($fi, 99999999999999999999);
> fwrite($fo, $rest);
>
> fclose($fi);
> fclose($fo);
>
--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti pδivittyvδ nettisarjis
spam@outolempi.net | rot13(xvzzb@bhgbyrzcv.arg)
Navigation:
[Reply to this message]
|