[Image-SIG] c code to python using gd

Necati DEMiR ndemir at demir.web.tr
Thu Mar 17 23:32:44 CET 2005


Hi,
i converted a c code to python. two of them uses gd, but they dont have 
the sama output :(

this is the c code;

#include <stdio.h>
#include <gd.h>
int main(){
    gdImagePtr in, out;
    FILE *inf, *outf;
    int c;
    int i, j;
    int color;
    int red, green, blue;
    int p1 = (255/3);
    int p2 = 2 * (255/3);
    inf = fopen("input.png","rb");
    in = gdImageCreateFromPng(inf);
    out = gdImageCreateTrueColor(gdImageSX(in), gdImageSY(in));
    gdImageColorAllocate(out, 255, 255, 255);
    for (i=0;i < gdImageSX(in); i++ ){
        for (j = 0; j< gdImageSY(in); j++){
            c = gdImageGetPixel(in, i, j);
            red = gdImageRed(in, c);
            green =  gdImageGreen(in, c);
            blue = gdImageBlue(in, c);

            if (p1 < red && red <= p2) red = 255 - red; else red = 0;
            if (p1< green && green <= p2) green = 255 - green; else 
green = 0;
            if (p1< blue && blue <= p2) blue = 255 - blue; else blue = 0;
            color = gdImageColorAllocate(out, red, green, blue);
            gdImageSetPixel(out, i, j, color);


        }
    }
    outf = fopen("output_c.png", "wb");
    gdImagePng(out, outf);

    gdImageDestroy(in);
    gdImageDestroy(out);

}

and this is the python code;

#!/usr/bin/python

import gd
inp = gd.image("input.png")
gd.gdMaxColors = 256 * 256 * 256
print inp.getClip()[1][0]+1,inp.getClip()[1][1]+1
output = gd.image((inp.getClip()[1][0]+1,inp.getClip()[1][1]+1),True)
p1 = 255+1 / 3
p2 = 2 * (255+1 / 3)
for i in range(0, inp.getClip()[1][0]+1):
    for j in range(0, inp.getClip()[1][1]+1):
        red = inp.red(inp.getPixel((i,j)))
        green = inp.green(inp.getPixel((i,j)))
        blue = inp.blue(inp.getPixel((i,j)))
        if (red > p1 and red <= p2):
             red = 255 - red
        else:
            red = 0
        if (green > p1 and green <= p2):
            green = 255 - green
        else:
            green = 0
        if (blue > p1 and blue <= p2):
            blue = 255 - blue
        else:
            blue = 0
        color = output.colorAllocate((red, green, blue))
        output.setPixel((i,j),color)

f=open("output_python.png","w")
output.writePng(f)
f.close()

and i am sending the input.png, output_c.png and output_python.png.

i couldn't find the problem.

-- 
-------------------------------------------------------------
Necati DEMiR
http://demir.web.tr

ndemir at demir.web.tr
0xAF7F745F
8525 9E78 F4EB 1DC6 51F3  1913 BB0D 7D9A AF7F 745F                                          
-------------------------------------------------------------

-------------- next part --------------
A non-text attachment was scrubbed...
Name: input.png
Type: image/png
Size: 314966 bytes
Desc: not available
Url : http://mail.python.org/pipermail/image-sig/attachments/20050317/609c34f9/input.png
-------------- next part --------------
A non-text attachment was scrubbed...
Name: output_c.png
Type: image/png
Size: 167747 bytes
Desc: not available
Url : http://mail.python.org/pipermail/image-sig/attachments/20050317/609c34f9/output_c.png
-------------- next part --------------
A non-text attachment was scrubbed...
Name: output_python.png
Type: image/png
Size: 521 bytes
Desc: not available
Url : http://mail.python.org/pipermail/image-sig/attachments/20050317/609c34f9/output_python.png


More information about the Image-SIG mailing list