well, here is the background. I have images of objects (cars, clothes, ...) with a white background in most of the cases
I have to build a function with PIL that takes away the background. it seems simple, just look for the "white" and make it transparent but the problem is in reality much more complex: 1) the image could contain some white inside the object (e.g. shoes with some white in between straps) 2) there are often pixels that are part of the background but have a colour different from white which leaves a few points throughout the image to be more concrete: here is a bit of code of what i've made so far def transparent(im): #i take all the images of the pixel pixels = list(im.getdata()) #i convert the image into png if im.mode != 'RGBA': im = im.convert('RGBA') #i create a new image with the same dimension with one unique layer for transparency width , height = im.size gradient = Image.new('L', (width,height)) white = { 'r' : 255 , 'g' : 255, 'b' : 255 } #i browse the pixels of the image for y in range(height): yp = y * width for x in range(width): xy = yp + x pix = pixels[xy] #the color of the current pixel c = { 'r' : pix[0] , 'g' : pix[1], 'b' : pix[2] } #i calculate the vectorial distance between the current color and the color white d = sqrt( pow((c['r']- white['r'] ),2) + pow((c['g'] - white['g']), 2) + pow((c['b'] - white['b']),2) ) if d < 5 : #if it is more or less white, i make the pixel transparent gradient.putpixel((x,y) , 0 ) else: #otherwise i show the color gradient.putpixel((x,y) , 255) after the layer of transparency of the new image is done, the algorithm works generally fine except there are some small but noticeable quality issues. i am just asking myself if there is maybe not a better approach either in terms of algorithms or even mathematics or maybe refine the algorithm that i've create. anything would help. i know the function will not be 100% precise but I just hope the image can be presentable and that the image is homogenous. thank you in advance for your help. -- http://mail.python.org/mailman/listinfo/python-list